问题的强类型的局部视图

编程入门 行业动态 更新时间:2024-10-27 23:22:27
本文介绍了问题的强类型的局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I've a problem with Partial View. I am developing a blog in asp 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); %>

更多推荐

问题的强类型的局部视图

本文发布于:2023-11-16 00:02:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599149.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   局部   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!