MVC에서 어디로 리디렉션 할 것인지를 결정해야 할 책임이 있습니다. I 컨트롤러라고 생각하면이지만 잘 모르겠습니다.컨트롤러에서 조건부 리디렉션 ToAction?
WorkshopItem
의 만들기 동작에서 전달 된 ViewModel에서 새 WorkshopItem을 만든 다음 데이터베이스에 저장합니다. ViewModel의 일부는 SelectedCustomerId
과 CustomerName
입니다. 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");