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?
Your
ifstatement always evaluates to true. You are testing whethermodel.CountryIdequalsmodel.CountryIdwhich is always true:if (model.CountryId == model.CountryId). Also you are missing anelsestatement. It should be like this:Obviously you need to replace
1and2with the proper values.Personally I would write an HTML helper for this task to avoid the tag soup in the views:
And then in your view simply: