MVC 5中html.textbox的条件绑定 [英] Condional binding for html.textbox in MVC 5
本文介绍了MVC 5中html.textbox的条件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何基于以下视图中的某些条件绑定 html.textbox
的数据。
如果long_variable为0,我想要将空值分配给 Html.TextBox
,否则为long_variable中的值。
How do i bind the data for html.textbox
based on some condition in view like below.
If long_variable is 0, i want to assign empty to the Html.TextBox
, else the value which is in long_variable.
<%= Html.TextBox("long_variable", "", new { @class = "short"})%>
我的尝试:
i尝试如下,但我不知道确切的事情。
<%if(Model.BagRunTime == 0){ %>
<%= Html.TextBox(BagRunTime,,new {@class =short})%>
What I have tried:
i tried like below, but i don't know the exact thing to do.
<%if(Model.BagRunTime==0) { %>
<%= Html.TextBox("BagRunTime", "", new { @class = "short"})%>
推荐答案
尝试使用三元运营商 [ ^ ]:
Try using the ternary operator[^]:
<%= Html.TextBox("long_variable", long_variable == 0 ? "" : long_variable.ToString(), new { @class = "short"}) %>
这篇关于MVC 5中html.textbox的条件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文