【问题标题】:Html.ActionLink Title parameter as test + valueHtml.ActionLink 标题参数作为测试 + 值
【发布时间】:2012-08-21 07:13:26
【问题描述】:

我有这一行,但不确定 + 字符串部分。现在不正确:

 <p>
    <%: Html.ActionLink("TUK-JS-KPI-WE-" + ((DateTime)Eval(ViewBag.jobSortedReportDate)).ToString("yyyy-MM-dd"), "TradeUKKPIReportInstructions", "Report", new { @date = ViewBag.jobSortedReportDate }, 0)%>    
 </p>

如何将文本添加到值中?

【问题讨论】:

    标签: c# asp.net-mvc-3 html.actionlink


    【解决方案1】:

    如果jobSortedReportDate 包含字符串值,您应该将其转换为字符串,就像这样:

    <%: Html.ActionLink("TUK-JS-KPI-WE-" + (string)ViewBag.jobSortedReportDate, "TradeUKKPIReportInstructions", "Report", new { @date = ViewBag.jobSortedReportDate }, 0)%> 
    

    【讨论】:

    • 是的,谢谢,刚刚发现,但又出现错误,无法将类型 DateTime 转换为字符串..让我快速查看:)
    • 在这种情况下,将其转换为 DateTime 而不是字符串 (DateTime)ViewBag.jobSortedReportDate
    • 是的,没错,抱歉今天有点慢,学习晚了:) 我想更进一步,有这个: 但显然这是不正确的
    • 你绝对应该阅读更多关于强制转换和类型转换的内容;)((DateTime)ViewBag.jobSortedReportDate).ToString("yyyy-MM-dd")
    【解决方案2】:

    你也可以试试这个

    <%: Html.ActionLink(String.Format("TUK-JS-KPI-WE-{0}", (DateTime) ViewBag.jobSortedReportDate), "TradeUKKPIReportInstructions", "Report", new { @date = ViewBag.jobSortedReportDate }, 0)%>
    

    这是另一种选择

    <a href="@Url.Action("TradeUKKPIReportInstructions", new {Controller = "Report", @date = ViewBag.jobSortedReportDate })">@String.Format("TUK-JS-KPI-WE-{0}", ViewBag.jobSortedReportDate )</a>
    

    【讨论】:

    • 哎呀我忘记了 System.Format,但仍然出现错误:'System.Web.Mvc.HtmlHelper' 没有名为 'ActionLink' 的适用方法但似乎具有该名称的扩展方法。扩展方法不能动态调度。考虑强制转换动态参数或在没有扩展方法语法的情况下调用扩展方法。我必须添加:我的行在 view.aspx 中
    • 您需要在 String.Format 中转换为 DateTime 以使其与 ViewBag String.Format("TUK-JS-KPI-WE-{0}", (DateTime)ViewBag.jobSortedReportDate) 一起使用
    • 谢谢。 +1 给你。我将它与ViewBag.Title 一起使用,认为这是一个字符串。现在我知道了
    猜你喜欢
    • 2010-12-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2012-07-25
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多