我不断收到编译错误,并且找不到匹配的重载方法。我尝试了几种方法(变量、variable.toString)。以下是最新的尝试。
当我点击日历上的日期(例如:2)时,ActionLink 应发送查询字符串:“Index?day=2”。
@{ string dayAsString = startCount.ToString();}
<div><span>@Html.ActionLink(@startCount.ToString, "Index?day=" + dayAsString , "Event")</span></div>
最佳答案
这样做
<div>
<span>
@Html.ActionLink(startCount.ToString(), "Index", new { day = startCount })
</span>
</div>
最后一个参数创建一个具有属性day
和值startCount
的匿名对象。 ActionLink 知道使用属性名称和属性值将其转换为查询字符串。
更多详细信息请点击 http://msdn.microsoft.com/en-us/library/dd492936.aspx
编辑:
如果您想定位特定 Controller ,请执行此操作
@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
您也可以在智能感知中循环。
关于asp.net-mvc - 如何将变量附加到 ActionLink 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10231842/