控制器中的条件RedirectToAction?

时间:2013-04-11 22:05:46

标签: asp.net-mvc

我只是想知道在MVC中确定重定向所属的责任。我认为它是控制器,但我不确定。

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");

这工作正常,但现在我还想RedirectToAction取决于是否创建了客户记录,因为如果创建了客户,它只有Name并且我想要通过CustomerId重定向到客户控制器上的编辑操作(我认为我可以这样做)。我的问题是,这在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");

1 个答案:

答案 0 :(得分: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");