MVC Html.ActionLink removes empty querystring parameter from URL

江枫思渺然 提交于 2019-12-01 18:53:31

问题


I'm using the Html.ActionLink(string linkText, string actionName, object routeValues) overload to send some params to an Action Method..

Sometimes I need to pass an empty parameter value like: ?item1=&item2=value

I'm specifying the param in the anonymous type I create and pass as routeValues, but both null and string.Empty result in a URL that omits the empty item1 param.

new { item1 = null, item2 = "value" } 
new { item1 = string.Empty, item2 = "value" }
new { item1 = "", item2 = "value" }

// all result in:
?item2=value

I can create the URL manually but I want to use the helper method.

Suggestions?


回答1:


Create an EmptyParameter class as follows:

public class EmptyParameter
{
    public override string ToString()
    {
        return String.Empty;
    }
}

Then:

@Html.ActionLink("Action",
                 "Controller",
                  new { item1 = new EmptyParameter(), item2 = "value" });


来源:https://stackoverflow.com/questions/17702203/mvc-html-actionlink-removes-empty-querystring-parameter-from-url

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