Вопрос

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?!)

Это было полезно?

Решение 2

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.

Другие советы

It should be

if (Model.Count == 0)

You need == to compare. Also, I recommend using the property Count rather than the function Count().

Model.Count() Model.Count does not exist

-You can use two Views and return a different View in the controller when the count is bigger than 0.

-You could put the Action link name and Action method name in the Model

In the controller:

if (xx.Count()==0) 
    return View("first",model) 
else 
    return View("second",model). 

Or in the controller:

if (xx.Count() == 0 
{ 
    model.ActionMethodName = "Createfoo"; 
    model.ActionLinkName="Create foo"; 
} 
else 
{ 
    model.ActionMethodName = "foo2"; 
    model.ActionLinkName="Your foo2"; 
} 

In the view you can then use <%: Html.ActionLink(Model.ActionLinkName,Model.ActionMethodName)%>

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top