@using (Html.BeginForm())
{
int controlWidth=250;
@Html.TextBoxFor(m => m.studentFirstName, new { style = "width:@(controlWidth)px;" })
此代码呈现为
<input data-val="true" data-val-required="The Student First Name field is required." id="studentFirstName" name="studentFirstName" style="width:@(controlWidth)px;" type="text" value="" />
可以理解,我希望它呈现为
<input data-val="true" data-val-required="The Student First Name field is required." id="studentFirstName" name="studentFirstName" style="width:250px;" type="text" value="" />
我看过类似 ASP.NET MVC Razor Concatenation 的帖子这表明这种做法是正确的。你知道我做错了什么吗?
最佳答案
我认为你做不到。内联模板只能在 HTML 中使用,如 <li style="@(controlWidth)">
或者如果您的方法支持内联模板。请参阅http://vibrantcode.com/blog/2010/8/2/inside-razor-part-3-templates.html/关于如何编写支持内联模板的方法。
关于asp.net-mvc - 字符串中的 Razor 变量未正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12203087/