通过大规模的列表MVC3查看 [英] Pass Massive list to View in MVC3

查看:122
本文介绍了通过大规模的列表MVC3查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新建ASP.NET MVC(使用MVC3剃刀现在),我从控制器传递对象到视图困惑。具体来说,我与MVC3罗布科纳的有趣的海量(http://blog.wekeroad.com/helpy-stuff/and-i-shall-call-it-massive)试验和。我用博客作为一个简单的Web应用程序进行实验,搞乱。

New to ASP.NET MVC (using MVC3 with Razor now) and I'm confused on passing an object from the Controller to the View. Specifically, I'm experimenting with MVC3 and Rob Conery's interesting Massive (http://blog.wekeroad.com/helpy-stuff/and-i-shall-call-it-massive). I'm messing with a blog as a simple web application to experiment with.

HomeController.cs:

HomeController.cs:

public ActionResult Index()
        {
            var table = new DynamicModel("mydb", tableName: "Posts");
            //grab all the posts
            var posts = table.All();
            ViewData["Posts"] = posts;

            return View();
        }

这部分的伟大工程。但我怎么做到这一点?

This part works great. But how do I do this?

return View(posts);

如果我引用 @ posts.PostID 在View,它错误,并说,这是无效的。所以,我想创建一个针对DynamicModel一个强类型的视图,但帖子仍是不存在。

If I reference @posts.PostID in the View, it errors out and says it's not valid. So I tried creating a strongly typed view against the DynamicModel, but posts still wasn't there.

我知道我可以创建一个视图模型并输入针对该观点,并在那里插上我的数据。这更多的是理解如何控制器/视图交互。

I know I can create a ViewModel and type the view against that and plug my data in there. This is more about understanding how the Controller/View interact.

谢谢!

推荐答案

ASP NET MVC模板是解决方案。

首先,使用的ViewData 尤其是现在不鼓励在MVC 2.0和3.0,其中有很多更好的方法来传递状态视图。

First of all, using ViewData is discouraged especially now in MVC 2.0 and 3.0 where there are many better ways to pass state to the view.

强类型的观点是要走的路。但是,如果你有一个集合怎么传?嗯,你仍然可以创建为集合(或泛型列表)一个强类型的视图,但是标准的解决方案是使用ASP NET MVC模板(这是ASCX文件)的项目,而不是收集和调用的​​RenderPartial您父视图:

Strongly typed view is the way to go. But What if you have a collection to pass? Well, you still can create a strongly typed view for the collection (or generic list) but standard solution is to use ASP NET MVC templates (which are ascx files) for the item and not the collection and call RenderPartial on your parent view:

例如:

        <div>
            <% for (int i = 0; i < Model.Bars.Count; i++)
               {%>
            <h3>
                <a href="#">Bar
                    <%: String.Format("{0:g}", Model.Bars[i].DocumentDate.ToShortDateString())%></a></h3>
            <div>
                <%: Html.DisplayFor(m => m.Bars[i].Content)%>
            </div>
            <%}%>
        </div>

有关于这个话题的>。

There is a very good series on this topic here.

这篇关于通过大规模的列表MVC3查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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