【问题标题】:Inconsistent Asp.net MVC4 (Razor 2.0) Rendering if Write in html attribute如果写入 html 属性,则不一致的 Asp.net MVC4 (Razor 2.0) 呈现
【发布时间】:2014-12-09 17:46:07
【问题描述】:

在 cshtml 视图中(Razor 2.0)我有代码:

<input type="text" value="@if (true) { Write("simpletext"); } " />

在 Razor 2.0 (MVC4) 生成的结果 html 中,我得到了:

<input type="text"simpletext value=" " />

在 Razor 1.0 (MVC3) 中代码是正确的:

<input type="text" value="simpletext" />

似乎渲染处理器等待报价关闭,然后将属性添加到结果流。

在这种情况下,如何将 razor 2.0 配置为 razor 1.0? 谢谢。

【问题讨论】:

    标签: asp.net-mvc-4 razor-2


    【解决方案1】:

    为什么不使用 Razor 提供的 HTML Helpers?我认为这将通过使用 HTML Helpers 有所帮助

    @if (true) { @Html.Encode("SimpleText")}
    

    【讨论】:

      【解决方案2】:

      尽量不要使用Write函数,而是直接在三元if中使用字符串

      <input type="text" value="@(true ? "simpletext" : "")" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多