如何解决以空/空字符串为参数的调用控制器操作
我的控制器中有一个需要2-3个参数的操作,如下所示。
public ActionResult MyAction(string str1,string str2,string str3)
{
// code here
}
该操作将使用一些有效值作为参数调用,结果将显示在视图中。现在,我想再次调用相同的操作,并希望将空/空字符串值作为参数传递。但是当我这样调用动作时:
@Html.ActionLink("Click","MyAction","MyController")
路由模板如下:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default","{str1}/{str2}/{str3}",new {controller = "MyController",action = "MyAction",str1 = UrlParameter.Optional,str2 = UrlParameter.Optional,str3 = UrlParameter.Optional}
);
}
我得到了与参数相同的值,如何使用空/空值作为参数来调用动作?
解决方法
尝试此代码
@Html.ActionLink("Click","MyAction","MyController",new { str1 = "",str2 = "",str3 = "" })
,
您可以在此处使用C#可选参数
public ActionResult MyAction(string str1="",string str2="",string str3=null)
{
// code here
}
@Html.ActionLink("link","About","Home")
public ActionResult MyAction(string str1="",string str3=null)
{
//values
str1=="";
str2=="";
str3==null;
}
@Html.ActionLink("link","Home",new { str1="a",str2="b",str3="c" })
public ActionResult MyAction(string str1="",string str3=null)
{
//values
str1=="a";
str2=="b";
str3=="c";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。