从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>