如何返回在ASP.NET MVC3控制器的局部视图? [英] How to return a partial view in a controller in ASP.NET MVC3?

查看:149
本文介绍了如何返回在ASP.NET MVC3控制器的局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器和它的一个方法(行动)访问我的项目数据库。这方法检查的项目类型。我怎么只显示如果从我的数据库检索的项目是一种特定类型的我的局部视图?

控制器动作例如:

 公众的ActionResult CheckItem(科科模型)
{
    VAR项目= db.Items.Where(项目=> item.Number == model.Number)。首先();
    如果(==项目。形式昂贵)
    {
       //显示局部视图(能够在我的观点我的一个局部视图)
    }
}


解决方案

您可以返回PartialView作用的结果:

 公众的ActionResult CheckItem(科科模型)
{
    VAR项目= db.Items.Where(项目=> item.Number == model.Number)。首先();
    如果(==项目。形式昂贵)
    {
        返回PartialView(下称部分的名称,someViewModel);
    }    ...
}

现在控制器动作将返回部分HTML。显然,这意味着你可能需要使用AJAX,以调用该控制器操作,否则你将得到局部视图替换当前浏览器窗口。在AJAX成功回调,你可以重新注入部分HTML DOM中看到更新。

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

解决方案

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.

这篇关于如何返回在ASP.NET MVC3控制器的局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆