MVC dynamic routeValues for ActionLinks

社会主义新天地 提交于 2019-12-05 10:42:40

How about something like this?

Model:

public class AViewModel
{

    public string Id1 { get; set; }
    public string Id2 { get; set; }
    public string Id3 { get; set; }

    public RouteValueDictionary GetRouteValues()
    {
        return new RouteValueDictionary( new { 
            Id1 = !String.IsNullOrEmpty(Id1) ? Id1 : String.Empty,
            Id2 = !String.IsNullOrEmpty(Id2) ? Id2 : String.Empty,
            Id3 = !String.IsNullOrEmpty(Id3) ? Id3 : String.Empty
        });
    }
}

View:

<%: Html.ActionLink("EDIT", "Action", "Controller", Model.GetRouteValues())%>

You can now reuse them as much as you like and only ever have to change them in one place.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!