如何将变量追加到一个ActionLink的字符串?(How do I append a variab

2019-07-29 01:38发布

我不断收到一个编译错误,并不能找到匹配的重载方法。 我已经尝试了几种方法(变量,variable.toString)。 下面是最新的尝试。

当我对天点击(例如:2),日历上的ActionLink的应该发送的查询字符串:“指数天= 2”。

@{ string dayAsString = startCount.ToString();}
<div><span>@Html.ActionLink(@startCount.ToString, "Index?day=" + dayAsString , "Event")</span></div>

Answer 1:

做这个

<div>
    <span>
        @Html.ActionLink(startCount.ToString(), "Index", new { day = startCount })
    </span>
</div>

最后一个参数创建一个与属性的匿名对象day和价值startCount 。 ActionLink的知道到转换成使用属性名称和属性值的查询字符串。

更多详细的http://msdn.microsoft.com/en-us/library/dd492936.aspx

编辑:

如果您要针对特定​​的控制器,这样做

@Html.ActionLink(startCount.ToString(), "Index", new { controller = "Event", day = startCount })

你也可以这样做

@Html.ActionLink(startCount.ToString(), "Index", "Event", new { day = startCount }, null)

但我不喜欢传球null作为参数。

这里的所有重载的列表: http://msdn.microsoft.com/en-us/library/dd505040.aspx

你也可以在智能感知周期。



Answer 2:

这应该工作

@Html.ActionLink(@startCount.ToString,"Index","Yourcontroller",new { day=@startCount.ToString()} , null)

与控制器名称替换Yourcontroller



文章来源: How do I append a variable to a string in an ActionLink?