Search code examples
c#asp.net-mvcviewactionlink

Syntax- If statement within view for ActionLink


I have a problem that I would like a solution to, with my many attempts I cannot seem to find a fix.

Problem I have an two ActionLinks that I would like to manipulate in my View.

Scenario In a strongly typed list view...If a user has not entered in their foo, then they will have the option to create a new foo. If a user has entered their foo, then they will have the only option to create a new foo2. (This would of course be represented in an if else condition)

Please can someone direct me to a better solution than my attempt below.

 <% if (Model.Count() = 0)
    { %>
       <p>
       <%: Html.ActionLink("Create foo", "Createfoo") %> 
      </p>
<% } else if (Model.Count() != 0)
    { %>
           <p>
           <%: Html.ActionLink("Your foo2", "foo2") %>
          </p>
<% } %>
// table logic
<% foreach (var item in Model) { %>
<td>
<%: Html.DisplayFor(modelItem => item.foo_id) %>
</td>
//more table logic blah blah blah

Please can someone advise how I can fix this problem? (Or alternative?!)


Solution

  • After looking into this further I have found that it does work, just needed to add () to the model.count.

    <% if (Model.Count() == 0)
            { %>
               <p>
                      <%: Html.ActionLink("Create foo", "Createfoo") %> 
              </p>
    
    <% }
        else if (Model.Count() >= 1)
        { %>
               <p>
               <%: Html.ActionLink("Your foo2", "foo2") %>
              </p>
    <% } %>
    

    Thanks to all who looked into this for me.