Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor - Can I make an optional model for a view?

I'm working with MVC3 with Razor for first time, and I have a partial view which is used in many other places and has no model. And now I need it, can I create an optional model? If it is passed, then I'll use it, otherwise I'll leave the default behavior.

[Update]

I'd like to call it like this:

@Html.Partial("_myPartialView")

or this:

@Html.Partial("_myPartialView", "Some string")

(the partial view model is a string)

Is this possible?

like image 678
kerzek Avatar asked Dec 22 '22 00:12

kerzek


1 Answers

@model FooBar
@if (Model != null)
{
    <div>@Model.SomeProperty</div>
}
else
{
    <div>No model passed</div>
}

UPDATE:

After showing the way you are calling your partial, here's what you could do:

@Html.Partial("_myPartialView", null, new ViewDataDictionary())
@Html.Partial("_myPartialView", "Some string")
like image 116
Darin Dimitrov Avatar answered Jan 05 '23 07:01

Darin Dimitrov