目前,我的oracle表的“启用”和“禁用”值为Y / N。编辑行时,下拉列表将显示“启用”和“禁用”。
<div class="form-group">
@Html.LabelFor(model => model.FLAG, htmlAttributes: new { @class ="control- label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("FLAG", new List<SelectListItem>{new SelectListItem{Text ="Enable",Value="Y",Selected=true},
new SelectListItem{Text ="Disable",Value="N"}})
@Html.ValidationMessageFor(model => model.FLAG, "", new { @class = "text-danger" })</div></div>
我的问题:
在显示记录的索引页中,显示“ Y”列的值。我想在索引页面中显示“启用”和“禁用”,而不是值“ Y”或值“ N”。有没有像dropdownlist这样的html helper,但是有一个标签,该标签显示了基于值的自定义文本,就像我对SelectListItem所做的那样?
您可以为此目的编写自定义帮助程序。您可以使用以下方法将其添加到视图内部:
// You can use Char type instead
@helper replaceValue(String val){
if(val.ToLower().Equals("y"){
<span style="color:green"> Enabled</span>
}
else if(val.ToLower().Equals("n"){
<span style="color:red"> Disabled</span>
}
else{
<span> N/A</span>
}
}
或者您可以定义为扩展方法,例如
public static class Extensions{
public static MvcHtmlString replaceVal(this HtmlHelper html, [And your parameters]){
... // You logic here
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句