Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

asp.net mvc - Route with Two optional parameters in MVC3 not working

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)
{

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...