asp.net-mvc – 如何在MVC视图中构造if语句
发布时间:2020-09-21 14:13:00 所属栏目:asp.Net 来源:互联网
导读:希望这个问题是快速和无痛的 我有一个mvc视图,我想根据if语句显示两个值之一.这就是我在观点本身: %if (model.CountryId == model.CountryId) % %= Html.Encode(model.LocalComment)% %= Html.Encode(model.IntComment)% 如果真实显示model.L
希望这个问题是快速和无痛的 我有一个mvc视图,我想根据if语句显示两个值之一.这就是我在观点本身: <%if (model.CountryId == model.CountryId) %> <%= Html.Encode(model.LocalComment)%> <%= Html.Encode(model.IntComment)%> 如果真实显示model.LocalComment,如果是false显示模型.IntComment. 这不能用于显示两个值.我究竟做错了什么? 解决方法您的if语句总是评估为true.你正在测试model.CountryId是否等于model.CountryId,它始终为true:if(model.CountryId == model.CountryId).你也缺少一个else语句.应该是这样的:<%if (model.CountryId == 1) { %> <%= Html.Encode(model.LocalComment) %> <% } else if (model.CountryId == 2) { %> <%= Html.Encode(model.IntComment) %> <% } %> 显然你需要用正确的值替换1和2. 就个人而言,我将为此任务编写一个HTML帮助程序,以避免视图中的标签汤: public static MvcHtmlString Comment(this HtmlHelper<YourModelType> htmlHelper) { var model = htmlHelper.ViewData.Model; if (model.CountryId == 1) { return MvcHtmlString.Create(model.LocalComment); } else if (model.CountryId == 2) { return MvcHtmlString.Create(model.IntComment); } return MvcHtmlString.Empty; } 然后在你的看法只是: <%= Html.Comment() %> (编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET通过自定义函数实现对字符串的大小写切换功能
- 回收ASP.NET应用程序是否会激起用户的兴趣?
- asp.net-mvc – 是否有一个ASP MVC与JSTL标签等效?
- .net – 加密ApplicationServices ConnectionString
- QueryString与ASP.NET MVC 6锚点标签助手
- 增加ASP.NET站点的executionTimeout和maxRequestLength是否
- asp.net – “’Microsoft.Jet.OLEDB.4.0’提供程序未在本地
- asp.net-mvc-3 – 如何将复选框绑定到mvc3中的viewmodel
- 我如何让Fiddler捕获我的MVC应用程序向我的ASP.NET Web API
- ASP.NET 程序中删除文件夹导致session失效问题的解决办法分