MVC Html.ActionLink从URL删除空的querystring参数 [英] MVC Html.ActionLink removes empty querystring parameter from URL

查看:71
本文介绍了MVC Html.ActionLink从URL删除空的querystring参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Html.ActionLink(string linkText, string actionName, object routeValues)重载将一些参数发送给Action方法.

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

有时我需要传递一个空的参数值,例如:?item1=&item2=value

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

我以我创建并作为routeValues传递的匿名类型指定参数,但是nullstring.Empty都导致URL省略了空的item1参数.

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

我可以手动创建URL,但是我想使用helper方法.

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

建议?

推荐答案

创建一个EmptyParameter类,如下所示:

Create an EmptyParameter class as follows:

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

然后:

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

这篇关于MVC Html.ActionLink从URL删除空的querystring参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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