How to use ternary operator in razor view?

2019-09-20 18:11发布

I am using MVC 4 razor view . here i want to disable the textboxfor if the "ERAGOGType" is "None" else if the value is not "None" then enable it

My view razor code

@{
if ((Model.ERAGOGType == "None"))
{   
    @Html.LabelFor(model => model.ERAGOGCode)
    @Html.TextBoxFor(model => model.ERAGOGCode, new { @disabled = true })
}
else
{
    @Html.LabelFor(model => model.ERAGOGCode)
    @Html.TextBoxFor(model => model.ERAGOGCode)
}}

How can i achieve this by using a ternary operator ? plz help

Thanks in advance

1条回答
Deceive 欺骗
2楼-- · 2019-09-20 18:17

You could try:

@Html.TextBoxFor(model => model.ERAGOGCode, Model.ERAGOGType == "None" ? new { @disabled = true } : null)
查看更多
登录 后发表回答