ASP.NET MVC - 更新多个部分/视图

时间:2009-09-30 15:58:28

标签: asp.net-mvc

你们如何更新(让我们说)控制器动作中的两个部分进入视图?例如,您有一个左侧导航部分用于链接,右侧是您的主要内容。您的主要内容具有绑定的ViewModel。你们的导航页面有一个单独的ViewModel吗?如果是这样,那么每次具有主内容视图模型和左侧导航ViewModel时,您是否创建了更大的ViewModel?

3 个答案:

答案 0 :(得分:0)

我会为导航页面(例如CategoryListViewModel)创建一个ViewModel,为内容页面创建一个ViewModel(例如ProductListViewModel)。然后在基本控制器中加载CategoryListViewModel


public abstract class BaseController : Controller
{
    var _categoryRepository = new CategoryRepository();

    protected BaseController()
    {
        ViewData["Categories"] = _categoryRepository.GetCategories();
    }
}

public class ProductsController : BaseController
{
    var _productRepository = new ProductRepository();

    public ActionResult Index()
    {
        return View(_productRepository.GetProducts());
    }
}

然后在MasterPage中阅读ViewData中的类别,并在内容页面中阅读ModelViewPage的产品。

答案 1 :(得分:0)

您可以将页面分成不同的独立部分,看看这个 第here页。在这里你必须使用Html.RenderAction() 从codeplex下载ASP.NET MVC Futures程序集。

答案 2 :(得分:-1)

实际上我最终还是躲到了this solution。谢谢大家的意见!