How to use ternary operator in razor view?

2019-09-20 17:54发布

问题:

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:

You could try:

@Html.TextBoxFor(model => model.ERAGOGCode, Model.ERAGOGType == "None" ? new { @disabled = true } : null)