How to use ternary operator in razor view?

你说的曾经没有我的故事 提交于 2020-01-26 04:34:07

问题


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)


来源:https://stackoverflow.com/questions/23757782/how-to-use-ternary-operator-in-razor-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!