为导航创建视图模型 [英] Create ViewModel for Navigation

查看:149
本文介绍了为导航创建视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个观点的MVC 4应用程序。即产品,配方,Distrubutors&安培;商店。

每个视图是根据各地的典范。

让我们保持简单,并说,我所有的控制器通过了类似的观点模型,看起来像我的产品的行动:

 公众的ActionResult指数()
{
    返回查看(db.Ingredients.ToList());
}

好了,所以这是好的,没有问题。但现在,我的所有网页的工作,我想改变我的导航(它的下拉列表中每个视图)加载在该模型中的项目。

所以,我有4个按钮(产品,食谱,Distrubutors&功放;存储)导航。

当您滚动每个按钮(假设我们滚动产品按钮),然后下拉将有产品上市。

要做到这一点,我需要创造一些类型的视图模型具有这些模型相结合的全部4个。很显然,我不能只切出PartialView每个导航元素,并使用

  @model IEnumerable的< GranSabanaUS.Models.Products>

和重复出来的产品为下拉,因为那么导航只会在产品视图工作,并在其他地方。

(后溶液)
,并YES罗文您在导航我创建的类型正确的,在这里看到:


解决方案

 公共类MenuContents
{
    公共IEnumerable的<产品> AllProducts {搞定;组; }
    公共IEnumerable的< Recepies> AllRecepies {搞定;组; }
    公共IEnumerable的<分销商和GT; AllDistributors {搞定;组; }
    公共IEnumerable的<存储> AllStores {搞定;组; }    私人XXXDb分贝=新XXXUSDb()    公共无效PopulateModel()
    {
        AllProducts = db.Products.ToList();
        AllRecepies = db.Recepies.ToList();
        AllDistributors = db.Distributors.ToList();
        AllStores = db.Stores.ToList();
    }
}

然后在您的控制器

 公众的ActionResult PartialWhatever()
{
    MenuContents模式=新MenuContents();
    model.PopulateModel();    返回PartialView(PartialViewName模型);
}

然后在你的局部视图

  @Model MenuContents......做任何这里

I have an MVC 4 application with several views. I.e. Products, Recipes, Distrubutors & Stores.

Each view is based around a model.

Let's keep it simple and say that all my controllers pass a similar view-model that looks something like my Product action:

public ActionResult Index()
{
    return View(db.Ingredients.ToList());
}

Ok so this is fine, no problems. But now that all of my pages work I want to change my navigation (which has dropdowns for each view) to load the items in that model.

So I would have a navigation with 4 Buttons (Products, Recipes, Distrubutors & Stores).

When you roll over each button (let's say we roll over the products button) then a dropdown would have the Products listed.

To do this I need to create some type of ViewModel that has all 4 of those models combined. Obviously I can't just cut out a PartialView for each navigation element and use

@model IEnumerable<GranSabanaUS.Models.Products>

And repeat out the Products for that dropdown, because then that navigation would only work in the Product View and nowhere else.

(After the solution) AND YES ROWAN You are correct in the type of nav I am creating, see here:

解决方案

public class MenuContents
{
    public IEnumerable<Products> AllProducts { get; set; }
    public IEnumerable<Recepies> AllRecepies { get; set; }
    public IEnumerable<Distributors> AllDistributors { get; set; }
    public IEnumerable<Stores> AllStores { get; set; }

    private XXXDb db = new XXXUSDb();

    public void PopulateModel()
    {
        AllProducts = db.Products.ToList();
        AllRecepies = db.Recepies.ToList();
        AllDistributors = db.Distributors.ToList();
        AllStores = db.Stores.ToList();
    }
}

Then in your controller

public ActionResult PartialWhatever()
{
    MenuContents model = new MenuContents();
    model.PopulateModel();

    return PartialView("PartialViewName", model);
}

Then in your partial view

@Model MenuContents

... do whatever here

这篇关于为导航创建视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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