Return a partial view from a controller?
When the partial takes a different model than the method you're including it in you need to use the overload that takes a model parameter and supply the model for the view. By default it uses the same model as the including view. Typically you only need the path if it's in a different, non-shared folder. If it's in the same controller's folder, using just the name should do the trick.
@Html.Partial("_Address", Model.Address)
On the other hand, if you're asking how do I get the partial view from an action included in my page, then you want to use the Action
method instead of the Partial
method.
@Html.Action("Address")
EDIT
To make the partial work you need to pass a Contact
model to the contact view.
public ActionResult Contact()
{
var contact = new Contact
{
Address = new Address
{
Line1 = "111 First Ave N.",
Line2 = "APT 222",
City = "Miami",
State = "FL",
Zip = "33133"
}
}
return View(contact);
}
demo for you:
public ActionResult Update(Demo model)
{
var item = db.Items.Where(item => item.Number == model.Number).First();
if (item.Type=="EXPENSIVE")
{
return PartialView("name Partial", someViewModel);
}
}