2013-04-11 3 views
0

MVC에서 어디로 리디렉션 할 것인지를 결정해야 할 책임이 있습니다. I 컨트롤러라고 생각하면이지만 잘 모르겠습니다.컨트롤러에서 조건부 리디렉션 ToAction?

WorkshopItem의 만들기 동작에서 전달 된 ViewModel에서 새 WorkshopItem을 만든 다음 데이터베이스에 저장합니다. ViewModel의 일부는 SelectedCustomerIdCustomerName입니다. SelectedCustomerId가 비어 있고 이름이 비어있는 경우 default customer 엔티티를 가져 와서 항목과 연결합니다. Id가 비었지만 이름이 아닌 경우 사용자가 고객을 검색했지만 일치하는 항목이 없으므로 값을 가져 와서 새 고객 레코드를 만들고 첨부합니다.

[NHibernateActionFilter] 
[HttpPost] 
public ActionResult Create(WorkshopItemCreateViewModel model) 
{ 
    try 
    { 
     Customer customer = null; 

     if (model.SelectedCustomerId == new Guid() && 
       !string.IsNullOrWhiteSpace(model.CustomerName)) 
      customer = CreateNewCustomer(model.CustomerName); 
     else if (model.SelectedCustomerId == new Guid() && 
       string.IsNullOrWhiteSpace(model.CustomerName)) 
     { 
      // Assign the System Valued customer if no customer was selected. 
      var id = Guid.Parse(ConfigurationManager.AppSettings["ValuedCustomerId"]); 
      customer = Session.QueryOver<Customer>() 
           .Where(c => c.Id == id) 
           .SingleOrDefault(); 
     } 

     // other stuff 
     return RedirectToAction("Index"); 

는 잘 작동하지만, 지금은 고객이 생성 된 경우에만 Name을 가지고 있으며, 내가 좋아하는 것 때문에 고객 레코드를 만들거나되었는지 여부에 따라 RedirectToAction 편집로 리디렉션으로도 원하는 CustomerId를 통과 한 Customer Controller에 대한 액션 (내가 할 수 있다고 생각하는). 내 질문은 이것이 MVC에서하는 것이 타당한가 아니면 이것이 다른 곳에서 책임이되어야 하는가이다.

이 같을 것이다 :

[NHibernateActionFilter] 
[HttpPost] 
public ActionResult Create(WorkshopItemCreateViewModel model) 
{ 
    try 
    { 
     Customer customer = null; 
     bool newCustomer = false; 
     if (model.SelectedCustomerId == new Guid() && 
       !string.IsNullOrWhiteSpace(model.CustomerName)) 
     { 
      customer = CreateNewCustomer(model.CustomerName); 
      newCustomer = true; 
     } 
     else if (model.SelectedCustomerId == new Guid() && 
       string.IsNullOrWhiteSpace(model.CustomerName)) 
     { 
      // Assign the System Valued customer if no customer was selected. 
      var id = Guid.Parse(ConfigurationManager.AppSettings["ValuedCustomerId"]); 
      customer = Session.QueryOver<Customer>() 
           .Where(c => c.Id == id) 
           .SingleOrDefault(); 
     } 

     // other stuff 
     if (newCustomer) 
      return RedirectToAction("Edit", "Customer", new {id=customer.Id}); 
     else 
      return RedirectToAction("Index"); 

답변

3

은 물론, 컨트롤러 콘텐츠를 반환하고 적절한 조치로 리디렉션의 책임을 유지한다. 컨트롤러를 거의 교통 경찰이라고 생각할 수 있습니다. 어디로 가야 할지를 지시하고 적절한 장소로 적절한 물건을 보냅니다. 위 코드의 예는 다음과 같습니다.

if (model.SelectedCustomerId == new Guid() && !string.IsNullOrWhiteSpace(model.CustomerName)) 
    customer = CreateNewCustomer(model.CustomerName); 
    return RedirectToAction("Edit", new {id = customer.Id}); 
else if (model.SelectedCustomerId == new Guid() && string.IsNullOrWhiteSpace(model.CustomerName)){ 
    // Assign the System Valued customer if no customer was selected. 
    var id = Guid.Parse(ConfigurationManager.AppSettings["ValuedCustomerId"]); 
    customer = Session.QueryOver<Customer>().Where(c => c.Id == id).SingleOrDefault(); 
    return RedirectToAction("SomeOtherMethod"); 

    }  
     // other stuff 
return RedirectToAction("Index");