[英]IF ELSE html helper in razor view?
I will like to use IF ELSE statement in Razor view. 我想在Razor视图中使用IF ELSE语句。 Is it possible to use IF( html.helper ) then do something?
是否可以使用IF(html.helper)然后做一些事情? Or any suggestion?
还是有什么建议吗?
@using (Html.BeginForm())
{
<table>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.HiddenFor(m => m[i].Question_ID)
@Html.HiddenFor(m => m[i].Type)
@Html.DisplayFor(m => m[i].Question)
</td>
</tr>
<tr>
@if(@Html.DisplayFor(m=> m[i].Type =="Info_Text") **
{
<td>
//DO NOTHING
</td>
}
else
{
<td>
@Html.EditorFor(m => m[i].Answer)
</td>
}
</tr>
}
</table>
As I mentioned in my comment, you can test the value of m[i].Type
directly: 正如我在评论中提到的,你可以直接测试
m[i].Type
的值:
@if (m[i].Type == "Info_Text") {
<td></td>
} else {
<td>@Html.EditorFor(m => m[i].Answer)</td>
}
The reason you wouldn't test against the value of DisplayFor
is that it returns an MvcHtmlString
, not just a simple type like a string
or int
. 你不会测试
DisplayFor
的值的原因是它返回一个MvcHtmlString
,而不仅仅是一个简单的类型,如string
或int
。 You could do something like this if you ever find the need to compare to a DisplayFor
some day (and hopefully this makes it all make a little more sense): 如果你有一天发现需要与
DisplayFor
进行比较,你可以做这样的事情(希望这会让一切变得更有意义):
@if (Html.DisplayFor(m => m[i].Type) == new MvcHtmlString("Info_Text"))
Since you're in the process of learning MVC, you might also be interested in how you can customize the EditorFor
helper to do this automatically: http://www.hanselman.com/blog/ASPNETMVCDisplayTemplateAndEditorTemplatesForEntityFrameworkDbGeographySpatialTypes.aspx 由于您正在学习MVC,您可能还对如何自定义
EditorFor
帮助程序以自动执行此操作感兴趣: http : //www.hanselman.com/blog/ASPNETMVCDisplayTemplateAndEditorTemplatesForEntityFrameworkDbGeographySpatialTypes.aspx
Why do you have to use DisplayFor? 你为什么要使用DisplayFor? Do you have any particular reason?
你有什么特别的理由吗?
How about if you use 如果你使用怎么样
if(Model[i].Type =="Info_Text")
{
<td>
//DO NOTHING
</td>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.