如何在 MVC 视图中构造 if 语句

发布于 2024-10-09 22:02:21 字数 353 浏览 1 评论 0原文

希望这个问题快速且轻松,

我有一个 mvc 视图,我想根据 if 语句显示两个值之一。这就是我在视图本身中的内容:

 <%if (model.CountryId == model.CountryId) %>
        <%= Html.Encode(model.LocalComment)%> 
        <%= Html.Encode(model.IntComment)%>

如果 true 显示 model.LocalComment,如果 false 显示 model.IntComment。

这不起作用,因为我显示了两个值。我做错了什么?

Hopefully this question is quick and painless

I have a mvc view where i want to display either one of two values depending on an if statement. This is what I have in the view itself:

 <%if (model.CountryId == model.CountryId) %>
        <%= Html.Encode(model.LocalComment)%> 
        <%= Html.Encode(model.IntComment)%>

If true display model.LocalComment, if false display model.IntComment.

This doesn't work as I get both values showing. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落在眉间の轻吻 2024-10-16 22:02:21

您的 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) %>
<% } %>

显然你需要用正确的值替换 12

就我个人而言,我会为此任务编写一个 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() %>

Your if statement always evaluates to true. You are testing whether model.CountryId equals model.CountryId which is always true: if (model.CountryId == model.CountryId). Also you are missing an else statement. It should be like this:

<%if (model.CountryId == 1) { %>
    <%= Html.Encode(model.LocalComment) %> 
<% } else if (model.CountryId == 2) { %>
    <%= Html.Encode(model.IntComment) %>
<% } %>

Obviously you need to replace 1 and 2 with the proper values.

Personally I would write an HTML helper for this task to avoid the tag soup in the views:

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;
}

And then in your view simply:

<%= Html.Comment() %>
慢慢从新开始 2024-10-16 22:02:21

除了达林关于条件始终为真的观点之外,您可能还需要考虑使用条件运算符:(

<%= Html.Encode(model.CountryId == 1 ? model.LocalComment : model.IntComment) %>

当然,根据您的真实条件进行调整。)

就我个人而言,我发现这比<% %><%= %> 的大混合物。

Aside from Darin's point about the condition always being true, you might want to consider using the conditional operator:

<%= Html.Encode(model.CountryId == 1 ? model.LocalComment : model.IntComment) %>

(Adjust for whatever your real condition would be, of course.)

Personally I find this easier to read than the big mixture of <% %> and <%= %>.

殤城〤 2024-10-16 22:02:21

Asp.Net MVC 视图中的条件渲染

 <% if(customer.Type == CustomerType.Affiliate) %>
   <%= this.Html.Image("~/Content/Images/customer_affiliate_icon.jpg")%>
 <% else if(customer.Type == CustomerType.Preferred) %>
   <%= this.Html.Image("~/Content/Images/customer_preferred_icon.jpg")%>
 <% else %>
   <%= this.Html.Image("~/Content/Images/customer_regular_icon.jpg")%>  

Conditional Rendering in Asp.Net MVC Views

 <% if(customer.Type == CustomerType.Affiliate) %>
   <%= this.Html.Image("~/Content/Images/customer_affiliate_icon.jpg")%>
 <% else if(customer.Type == CustomerType.Preferred) %>
   <%= this.Html.Image("~/Content/Images/customer_preferred_icon.jpg")%>
 <% else %>
   <%= this.Html.Image("~/Content/Images/customer_regular_icon.jpg")%>  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文