How to return a partial view in a controller in ASP.NET MVC3?

16,792

You could return a PartialView action result:

public ActionResult CheckItem(Koko model)
{
    var item = db.Items.Where(item => item.Number == model.Number).First();
    if (item.Type=="EXPENSIVE")
    {
        return PartialView("name of the partial", someViewModel);
    }

    ...
}

Now the controller action will return partial HTML. This obviously means that you might need to use AJAX in order to invoke this controller action otherwise you will get the partial view replace the current browser window. In the AJAX success callback you could reinject the partial HTML in the DOM to see the update.

Share:
16,792
raberana
Author by

raberana

:D https://www.linkedin.com/in/raberana/

Updated on June 05, 2022

Comments

  • raberana
    raberana almost 2 years

    I have a controller and one of its methods (actions) access my database of items. That method checks the item type. How do I show my partial view only if the item retrieved from my database is of a specific type?

    Controller Action example:

    public ActionResult CheckItem(Koko model)
    {
        var item = db.Items.Where(item => item.Number == model.Number).First();
        if(item.Type=="EXPENSIVE")
        {
           //show partial view (enable my partial view in one of my Views)
        }
    }