asp.net mvc razor 变量设置不正确
asp.net mvc razor variables not correctly setting
我正在处理一个页面,我想根据条件将 class 添加到控件中。我尝试了下面的代码,但是当我在 cshtml 文件中添加动态 class 名称时,它以错误的方式呈现。有什么建议吗?
object TitleClass = (Model.Id != 0 && Model.IsApproved) ?
(object)new { @disabled = "disabled", @class = "headerBox" } : new{ @class = "headerBox"};
<div>
@Html.TextBoxFor(model => model.Title, new { @placeholder = "New Title", TitleClass })
@Html.ValidationMessageFor(model => model.Title)
</div>
呈现为
<input name="Title" id="Title" type="text" placeholder="New Demand Title" value="cpx appr with orders" data-val-required="Enter Demand Title" data-val="true" titleclass="{ disabled = disabled, class = headerBox }">
正在尝试将其呈现为
<input name="Title" id="Title" type="text" placeholder="New Demand Title" value="cpx appr with orders" data-val-required="Enter Demand Title" data-val="true" disabled = disabled class = headerBox >
尝试:
object attributes = (Model.Id != 0 && Model.IsApproved) ?
(object)new { @placeholder = "New Title", @disabled = "disabled", @class = "headerBox" } : new{ @placeholder = "New Title", @class = "headerBox"};
<div>
@Html.TextBoxFor(model => model.Title, attributes)
@Html.ValidationMessageFor(model => model.Title)
</div>
您需要将 placeholder
属性添加到 TitleClass
以避免创建 2 组新的匿名对象
object TitleClass = (Model.Id != 0 && Model.IsApproved) ?
(object)new { @placeholder = "New Title", @disabled = "disabled", @class = "headerBox" } : (object)new{ @placeholder = "New Title", @class = "headerBox"};
并将 TextBoxFor()
方法更改为
@Html.TextBoxFor(m => m.Title, TitleClass)
旁注:设置 disabled = "disabled"
意味着文本框的值不会 post 返回并且 属性 Title
在控制器中将为空
我正在处理一个页面,我想根据条件将 class 添加到控件中。我尝试了下面的代码,但是当我在 cshtml 文件中添加动态 class 名称时,它以错误的方式呈现。有什么建议吗?
object TitleClass = (Model.Id != 0 && Model.IsApproved) ?
(object)new { @disabled = "disabled", @class = "headerBox" } : new{ @class = "headerBox"};
<div>
@Html.TextBoxFor(model => model.Title, new { @placeholder = "New Title", TitleClass })
@Html.ValidationMessageFor(model => model.Title)
</div>
呈现为
<input name="Title" id="Title" type="text" placeholder="New Demand Title" value="cpx appr with orders" data-val-required="Enter Demand Title" data-val="true" titleclass="{ disabled = disabled, class = headerBox }">
正在尝试将其呈现为
<input name="Title" id="Title" type="text" placeholder="New Demand Title" value="cpx appr with orders" data-val-required="Enter Demand Title" data-val="true" disabled = disabled class = headerBox >
尝试:
object attributes = (Model.Id != 0 && Model.IsApproved) ?
(object)new { @placeholder = "New Title", @disabled = "disabled", @class = "headerBox" } : new{ @placeholder = "New Title", @class = "headerBox"};
<div>
@Html.TextBoxFor(model => model.Title, attributes)
@Html.ValidationMessageFor(model => model.Title)
</div>
您需要将 placeholder
属性添加到 TitleClass
以避免创建 2 组新的匿名对象
object TitleClass = (Model.Id != 0 && Model.IsApproved) ?
(object)new { @placeholder = "New Title", @disabled = "disabled", @class = "headerBox" } : (object)new{ @placeholder = "New Title", @class = "headerBox"};
并将 TextBoxFor()
方法更改为
@Html.TextBoxFor(m => m.Title, TitleClass)
旁注:设置 disabled = "disabled"
意味着文本框的值不会 post 返回并且 属性 Title
在控制器中将为空