强类型局部视图的问题

Iva*_*n90 0 asp.net-mvc

我有部分视图的问题.我正在asp.net mvc开发一个博客,我会在我的主页上列出一个类别列表,最后发表的帖子,最后的评论.我认为最好的解决方案是使用强类型的局部视图,并在每个局部视图中传递必要的模型.

我的问题是View ..在任何视图(连接到母版页的contentplaceholder)中的模型与部分视图中的模型冲突,我得到如下错误:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Blog.Models.Articoli]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Blog.Models.Categorie]'. 
Run Code Online (Sandbox Code Playgroud)

我在网上发现了一个肮脏的解决方案,它包括传递任何视图的模型,一些viewdata,每个模型都可以在局部视图中传递.但是这个解决方案不尊重DRY原则.因为你必须为每个动作重复这个代码!

所以,我的问题是:我可以创建一个包含局部视图模型的模型吗?如果,是的,以这种方式?

它存在另一种解决方案更简单吗?

感谢帮助

hun*_*ter 5

视图模型模式怎么样?

我创建了传递给我的视图的包装类,而不是我通常使用的任何对象

public class MyCreateUserView
{
    public User CreatingUser { get; set; }
    public MyPartialViewObject Blah { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在你的视图中写道:

public ActionResult CreateUser()
{
    MyCreateUserView createUser = new MyCreateUserView()
    {
        CreatingUser = GetUserFromSomewhere(),
        Blah = GetPartialViewObject();
    }

    return View(createUser);
}
Run Code Online (Sandbox Code Playgroud)

然后您的页面标题如下所示:

<%@ Page Language="C#" Inherits="ViewPage<MyCreateUserView>" %>
Run Code Online (Sandbox Code Playgroud)

当你渲染你的部分写作时:

<% Html.RenderPartial("../MyPartialViewObject ", Model.Blah); %>
Run Code Online (Sandbox Code Playgroud)