mannge
mannge

Reputation: 381

Comparing Model in Razor C#

I'm finally starting to learn MVC 3 with Razor. I've a problem with checking which model i'm passing along.

I need to convert this 3 kind of MVC 2 example code to Razor/MVC 3:

<li class="<%= Model is FooModel ? "active" : null %>"><a href="<%= Url.Action("Foo") %>Foo</a></li>
<li class="<%= Model is DooModel || Model is BooModel ? "selected" : null %>"><a href="<%= Url.Action("Doo") %>">Doo</a></li>


 <% if (Model is FooModel){ %>Foo<% } else if(Model is DooModel){ %>Doo<% } %>


 <% if (Model is FooModel)
    Html.RenderPartial("Foo");
    else if(Model is DooModel)
     Html.RenderPartial("Doo"); %>

Thanks for any kind of help.

Upvotes: 0

Views: 267

Answers (1)

gideon
gideon

Reputation: 19465

Something like this should work:

<li class="@(Model is FooModel ? "")"><a href="@(Url.Action("Foo"))">Foo</a></li>
<li class="@(Model is DooModel || Model is BooModel ? "selected")>
       <a href="@(Url.Action("Doo"))">Doo</a></li>

@if (Model is FooModel)
{ 
    //not sure what Foo is here
    //if it's just a string/text  just do:
    @:Foo
} 
else if(Model is DooModel) 
{
    @Doo //what is Doo here?
}

@if (Model is FooModel) {
    Html.Partial("Foo")
} else if(Model is DooModel)
{
    Html.Partial("Doo")
}

Notes: (1) You shouldn't return null, just don't return anything. (2) I'm not sure what you want inside your first and second block what Foo and Doo are and what you need to do with them.

Upvotes: 1

Related Questions