开发者

Route with nullable parameter problem

开发者 https://www.devze.com 2022-12-17 20:38 出处:网络
I have a page where you edit \"usergroups\". And I have this in my controller:开发者_如何学Go public ActionResult UsergroupEdit(int? usergroupID)

I have a page where you edit "usergroups". And I have this in my controller:

开发者_如何学Go
public ActionResult UsergroupEdit(int? usergroupID)

both edit and create on the same view.

and in global.asa:

 routes.MapRoute("AdminUsergroupEdit", "Admin/Usergroup/Edit/{usergroupID}",
            new
            {
                controller = "UsergroupsAdmin",
                action = "UsergroupEdit"
            }
                );

 routes.MapRoute("AdminUsergroupCreate", "Admin/Usergroup/Edit/",
            new
            {
                controller = "UsergroupsAdmin",
                action = "UsergroupEdit"
            }
                );

And when using this it works:

Html.ActionLink("Edit", "UsergroupEdit", new { usergroupID = Model.Usergroup.UsergroupID })

But this one doesnt match, whats the problem with this one?

Html.ActionLink("Create", "UsergroupEdit")

How can I make it match in both cases? since "UsergroupID" is nullable, but in both cases should match that action?

/M


Use Html.RouteLink instead of Html.ActionLink:

Html.RouteLink("Create", "AdminUsergroupCreate", "Create",
    new RouteValueDictionary { { "action", "UsergroupEdit" } } )

RouteLink is faster and never picks the wrong route.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号