简体   繁体   中英

How to load correctly a partial view in mvc?

In my mvc project i have a controller with the following actions:

public ActionResult Index()
    {
        return View(new List<Product>());
    }

The corresponding index view render a partial view with the master grid:

@model System.Collections.Generic.List<TbbModels.Domain.Product>
@Html.Partial("_ProdutoMasterGrid", Model)

This partial view have a submit button and a grid. The client needs to put some data in the form and submit it to that action:

 public ActionResult _ProdutoMasterGrid(string param)
    {
        return PartialView("_ProdutoMasterGrid",
        repository.Compare(param).ToList());
    }

But than i get the grid without the layout. How can i return a partial view with the layout?

You should explicitly return the correct view:

public ActionResult _ProdutoMasterGrid(string param)
{
    return View("Index",
    repository.Compare(param).ToList());
}

This ensures that when you do a post to this action, it returns the index view. I'm supposing here that repository.Compare return a List<Product> as well, since the type needs to match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM