路线在MVC3两个可选参数不工作 [英] Route with Two optional parameters in MVC3 not working

查看:102
本文介绍了路线在MVC3两个可选参数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序使用了以下类型的URL。


  

本地主机/管理/ userdetail / ID


  
  

本地主机/管理/ userdetail / ID /真正


  
  

本地主机/管理/ userdetail / ID /真/成功


下面是我的联系控制器


  

布尔inSaveAction,串状态是可选


  [授权]
    公众的ActionResult UserDetail(字符串ID,布尔inSaveAction,串状态)
    {
    }    [HttpPost,授权,ValidateAntiForgeryToken]
    公众的ActionResult SaveUserDetail(UserDetailViewModel视图模型)
    {
        用户userToSave =新用户();
        AdminService.UpdateUser(userToSave);
        //这是调用上面的函数,它发送所有3 PARAMS
        返回RedirectToAction(UserDetail,新{ID = viewModel.Id,
                           inSaveAction = TRUE,状态=成功});
    }


  

以下情况下不能正常工作


  @ Html.ActionLink(显示名称,UserDetail,新{ID = Model.Id})

在Global.asax中

  routes.MapRoute(UserDetail
            UserDetail /(编号),
            新
            {
                控制器=管理,
                行动=UserDetail
                ID = UrlParameter.Optional
            }
         );

我也跟着<一个href=\"http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx\">http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

我怎样才能让inSaveAction&安培;至于我UserDetail行动可选参数状态?


解决方案

您错过在你的路线配置的参数。为了使具有可选不同的参数这项工作(如菲尔哈克的职位),您需要定义多个路由

  routes.MapRoute(UserDetail-WithStatus
                UserDetail / {ID} / {inSaveAction} / {}状态,
                 新
                 {
                     控制器=管理,
                     行动=UserDetail
                     //没什么可选
                 }
);routes.MapRoute(UserDetail-WithoutStatus
                UserDetail / {ID} / {} inSaveAction
                 新
                 {
                     控制器=管理,
                     行动=UserDetail
                     //没什么可选
                 }
);routes.MapRoute(UserDetail-WithoutSaveAction
                UserDetail /(编号),
                 新
                 {
                     控制器=管理,
                     行动=UserDetail
                     ID = UrlParameter.Optional
                 }
);

,然后创建链接:

  @ Html.ActionLink(链接,索引,管理,新的{ID = 1,inSaveAction =真,成功=成功},NULL)

您还需要设置可选的参数为空,否则你会得到异常,如果ID或inSaveAction丢失。

 公众的ActionResult UserDetail(INT?ID,布尔?inSaveAction,串状态)
{}

I have a following types of url used in my Application.

localhost/admin/userdetail/id

localhost/admin/userdetail/id/true

localhost/admin/userdetail/id/true/success

Here is my Admin Controller

bool inSaveAction, string status are optional

    [Authorize]
    public ActionResult UserDetail(string Id, bool inSaveAction, string status)
    {
    }

    [HttpPost, Authorize, ValidateAntiForgeryToken]
    public ActionResult SaveUserDetail(UserDetailViewModel viewModel)
    {
        User userToSave = new User();
        AdminService.UpdateUser(userToSave);
        //This is calling the above function as it sending all 3 params
        return RedirectToAction("UserDetail", new { Id = viewModel.Id, 
                           inSaveAction = true, status = "success" });
    }

Below case is not working

  @Html.ActionLink("DisplayName", "UserDetail", new { id = Model.Id })

In Global.asax

 routes.MapRoute("UserDetail",
            "UserDetail/{id}",
            new
            {
                controller = "Admin",
                action = "UserDetail",
                id = UrlParameter.Optional
            }
         );

I followed http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

How can i make inSaveAction & status as optional parameter for my UserDetail action?

解决方案

You're missing the parameters in your route config. In order to make this work with different parameters optional (as in Phil Haack's post), you need to define multiple routes

routes.MapRoute("UserDetail-WithStatus", 
                "UserDetail/{id}/{inSaveAction}/{status}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     // nothing optional 
                 }
);

routes.MapRoute("UserDetail-WithoutStatus", 
                "UserDetail/{id}/{inSaveAction}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     // nothing optional 
                 }
);

routes.MapRoute("UserDetail-WithoutSaveAction", 
                "UserDetail/{id}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     id = UrlParameter.Optional
                 }
);

And then create links with:

@Html.ActionLink("Link", "Index", "Admin", new { id = 1, inSaveAction = true, success = "success" }, null)

You'll also need to set the optional parameters as nullable, otherwise you'll get exceptions if id or inSaveAction are missing.

public ActionResult UserDetail(int? id, bool? inSaveAction, string status)
{

}

这篇关于路线在MVC3两个可选参数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆