如何在ActionLink中将变量附加到字符串? [英] How do I append a variable to a string in an ActionLink?

查看:89
本文介绍了如何在ActionLink中将变量附加到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到编译错误,找不到匹配的重载方法.我尝试了几种方法(variable,variable.toString).以下是最新的尝试.

I keep getting a Compilation Error and can't find matching overloaded method. I've tried a couple ways (variable, variable.toString). Below is the latest try.

当我在日历上单击日期(例如2)时,ActionLink应该发送查询字符串:"Index?day = 2".

When I click on the day (ex: 2) on the calendar the ActionLink should send the querystring: "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知道可以使用属性名称和属性值将其转换为查询字符串.

The last parameter creates an anonymous object with the property day and value startCount. ActionLink knows to convert that into a querystring using the property name and the property value.

此处有更多详细信息 http://msdn.microsoft.com/zh-CN/library/dd492936.aspx

如果要定位特定的控制器,请执行此操作

If you want to target a specific controller, do this

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

您也可以这样做

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

但我不喜欢将null作为参数传递.

but I don't like passing null as a parameter.

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

Here's a list of all the overloads: http://msdn.microsoft.com/en-us/library/dd505040.aspx

您也可以循环使用智能感知.

You can also just cycle in the intellisense.

这篇关于如何在ActionLink中将变量附加到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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