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

Deadly 提交于 2019-12-07 21:12:28

问题


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)
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/10596321/how-to-return-a-partial-view-in-a-controller-in-asp-net-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!