问题的强类型的局部视图 [英] Problem with strongly typed partial view

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

问题描述

我和局部视图的一个问题。
我在开发asp.net的MVC一个博客,我会在我的母版制作类,最后一篇,最新评论列表。
我认为最好的解决方法是使用强类型的局部视图,并在每个局部视图通过必要的典范。

I've a problem with Partial View. I am developing a blog in asp.net mvc and I would make in my masterpage a list of categories, last post, last comments. I think that the best solution is to use strongly typed partial view, and in each partial view pass the necessary model.

我的问题是,在查看模型。在任何视图(连接到母版的的ContentPlaceHolder)与在局部视图模型冲突进入,我得到这样一个错误:

MY problem is that the model in View.. in any view (connected to the masterpage's contentplaceholder) enter in conflict with the models in partial views and I get an error like this:

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]'. 

我在网上找到了一个肮脏的解决方案,包括通过togheter任何视图模型,一些可视数据,一个用于每个模型的局部视图通过。但这种方法不尊重DRY Principle..because你必须重复此code每个动作!

I found on web a dirty solution that consist of to pass togheter the model of any view, some viewdata, one for every model to pass in partial view. But this solution don't respect DRY Principle..because you must repeat this code for each action!

所以,我的问题是:我可以创建中还含有局部视图模型的模型?
如果是,以这种方式?

So, my question is: Can I create a model that contain also partial view's model? If, yes, in that way?

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

It Exist another solution more simple?

感谢您的帮助。

推荐答案

如何查看型号模式?

我创建传递给我的意见,而不是任何对象我通常会使用包装类

I've created wrapper classes that are passed to my views rather than whatever object I would normally use

public class MyCreateUserView
{
    public User CreatingUser { get; set; }
    public MyPartialViewObject Blah { get; set; }
}

在你的看法写:

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

    return View(createUser);
}

那么你的页头看起来像这样:

Then your page header looks like so:

<%@ Page Language="C#" Inherits="ViewPage<MyCreateUserView>" %>

当你渲染你的部分写:

and when you render your partial write:

<% Html.RenderPartial("../MyPartialViewObject ", Model.Blah); %>

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

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