许多模型,查看许多局部视图 [英] Many models to view with many partial views

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

问题描述

我有一个包含许多部分意见认为,我需要传递给每一个匹配的模式。

I have a view that contains many partial views and I need to pass to each one the matching model.

我发现2种方法来做到这一点,但我不知道什么真正的方式,它应该做的事。

I've found 2 ways to do this, but I don't know whats the real way it should be done.


  1. 我想创建一个包含所有车型的性能和比我可以将模型发送到每个局部视图大类的。问题是,它很难类型化的,如果我需要传递的机型不同势组合,它不会适合。

  1. I thought of creating big class that contain all the models as properties and than i can send the models to each partial view. the problem is that its hard typed, and if i need to pass a diffrent combination of models it wont fit.

我虽然是有每个模型的方法带给我的每一个局部视图()GetMenuBar(等)模型的另一种方法。

The other way I though of is having a methods in each model that bring me the model for each partial view (GetMenuBar() and so on).

请告诉我正确的方式做呢?

Whats the right way doing that?

推荐答案

我的建议,去与选项1.我用我所有的主视图/多管窥场景。这很容易维护,因为每个部分都有它自己的视图模型。它使整个事情非常干净

My advice, go with Option 1. I use that with all of my Main View/Multiple Partial View scenarios. It's easy to maintain as each partial has it's own ViewModel. It keeps the whole thing nice and clean

我使用完全相同的设置,像这样:

I use the exact same setup like so:

public class MainViewModel {

    public Partial1ViewModel Partial1 [ get; set; }
    public Partial2ViewModel Partial2 [ get; set; }
    public Partial3ViewModel Partial3 { get; set; }
    public Partial4ViewModel Partial4 { get; set; }

    public MainViewModel() {}

    public MainViewModel() {
        Partial1 = new Partial1ViewModel();
        Partial2 = new Partial2ViewModel();
        Partial3 = new Partial3ViewModel();
        Partial4 = new Partial4ViewModel();
    }
}

每个 PartialViewXViewModel 是它自己的视图模型和如果需要的话可以在另一个视图中重用。

Each PartialViewXViewModel is it's own ViewModel and if need be can be reused in another view.

您的行动呈现可以看起来像这样:

Your Action that renders can look like so:

public ActionResult Index {
    var model = new MainViewModel();
    return View(model);
}

您查看

@model MainViewModel

<div>
    {@Html.RenderPartial("PartialOne", Model.Partial1)}
</div>


<div>
    {@Html.RenderPartial("PartialTwo", Model.Partial2)}
</div>


<div>
    {@Html.RenderPartial("PartialThree", Model.Partial3)}
</div>


<div>
    {@Html.RenderPartial("PartialFour", Model.Partial4)}
</div>

定义每个用户界面 PartialX 这样的:

@model Partial1ViewModel

//view html here

现在,他们使用的每个局部视图html和每个模型可以在任何地方使用。

Now, each Partial view html and each model that they use can be used anywhere.

的很大一部分,现在是,如果你有需要的只有这两个,你只需要创建一个新的视图模型重新present一个页面,像这样具体的看法:

The great part is now if you have a page that needs only 2 of these you just create a new ViewModel to represent that specific view like so:

public class OtherMainViewModel {

    public Partial2ViewModel Partial2 [ get; set; }
    public Partial4ViewModel Partial4 { get; set; }

    public OtherMainViewModel() {}

    public OtherMainViewModel() {
        Partial2 = new Partial2ViewModel();
        Partial4 = new Partial4ViewModel();
    }
}

而在像这样另一种观点认为使用它:

And use it in another view like so:

public ActionResult SomeOtherAction {
    var model = new OtherMainViewModel();
    return View(model);
}

而这完全可以接受的,也是preferred设计MVC战略,具有的ViewModels专门重新present什么看法需要和唯一它所需要的。

And that's perfectly acceptable and also the preferred design strategy in MVC, to have ViewModels that specifically represent what a view needs and only what it needs.

您可能需要使用不同的方法来填充你的模型寿。这里大部分建议使用Automapper。无论哪种方式,以上只是初始化在MainViewModel的构造函数的PartialViewXModels。这并不一定是你的情况下,如果您将这些模型与您的DB数据。你希望自己该战略。这将在这里工作:

You may want to use a different method for populating your models tho. Most here would recommend using Automapper. Either way, the above just initializes the PartialViewXModels in the constructor of the MainViewModel. That won't necessarily be your case if you are populating those models with data from your DB. You would want your own strategy for that. This would work here:

public ActionResult Index {
    var model = new MainViewModel();
    model.Partial1 = GetPartial1Data(); // this method would return Partial1ViewModel instance
    model.Partial2 = GetPartial2Data(); // same as above for Partial2ViewModel
    ...
    return View(model);
}

这一切只会让你开始的设计,您可以将其调整到你的心内容: - )

This all would just get you started with the design, you can tweak it to your hearts content :-)

这篇关于许多模型,查看许多局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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