从MVC 3迁移到MVC 5后,Razor语法绑定属性不起作用

时间:2015-11-30 16:21:26

标签: asp.net asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-5

从MVC 3迁移到MVC 5后,html元素中的设置属性的构建停止工作。它不是在标记中,而是在原始文本class="button bold"

上呈现在屏幕上
@foreach(var part in Model){
     <div attr="@part.Id">...</div> //output: <div>...</div>
     <div class="@part.ClassName">...</div> //output: <div>...</div>
}

只有以data-开头的自定义属性正在运行

<div data-attr="@part.Id">...</div> //output: <div data-attr="7">...</div>

解决方法是使用@Html.Raw方法

<div @Html.Raw(string.Format(@"class=""{0}""",part.ClassName))>...</div> //output: <div class="className">...</div>

任何想法发生了什么以及如何恢复以前的行为?

配置

<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />

运行时部分

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
</assemblyBinding>

2 个答案:

答案 0 :(得分:3)

这将解决它。

<div class= "@(Model.ClassName)">...</div>

答案 1 :(得分:1)

一张图片胜过千言万语。这三个屏幕截图显示没有空格

行动方法

enter image description here

查看

enter image description here

网络浏览器,检查元素

enter image description here