HTML辅助条件不工作 [英] Condition of html helper not working
本文介绍了HTML辅助条件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要disabled属性的基础上一个条件被添加到一个文本框
@ Html.TextBoxFor(M = GT; m.kidsNumber,新{!(Model.kidsDropDown =2)@disabled:}?)
解决方案
使用
@ Html.TextBoxFor(M = GT; m.kidsNumber,Model.kidsDropDown =2新{禁用=禁用}!?空)
请注意,如果你还需要添加多个属性,那么它需要的格式(其中属性转换为对象
@ Html.TextBoxFor(M = GT;!?m.kidsNumber,Model.kidsDropDown =2(对象)新的{@disabled =已禁用,@类=表格控}:(对象)新{@类=表格控})
如果您有使用相同的属性集的多个文本框,你可以在视图分配这些变量
@ {
对象数= {新@type =数字,@类=表格控};
反对disabledNumber =新的{@disabled =已禁用,@类=表格控};
}
和形式
@ Html.TextBoxFor(M = GT; m.kidsNumber,Model.kidsDropDown =2disabledNumber!?号)
@ Html.TextBoxFor(M => m.anotherProperty,AnotherCondition disabledNumber:数字)
.....
I want the disabled attribute be added to a textbox based on a condition
@Html.TextBoxFor(m => m.kidsNumber, new { (Model.kidsDropDown != "2") ? "@disabled" : ""})
解决方案
Use
@Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? new {disabled = "disabled"} : null )
Note also if you need to add multiple attributes, then it needs to be in the format (where the attributes are cast to object
@Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? (object)new { @disabled = "disabled", @class="form-control" } : (object)new { @class="form-control" })
If you have multiple textboxes that use the same sets of attributes, you can assign these to variables in the view
@{
object number = new { @type = "number", @class="form-control" };
object disabledNumber = new { @disabled = "disabled", @class="form-control" };
}
and in the form
@Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? disabledNumber : number)
@Html.TextBoxFor(m => m.anotherProperty, AnotherCondition ? disabledNumber : number)
.....
这篇关于HTML辅助条件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文