我见过很多关于此的问题,但我从未真正得到我需要的答案。
我正在将一个相当大的 Web 应用程序从 Web 表单转换为 MVC,一段时间后我遇到了将数据传递到视图的问题。 在Action中我执行代码:
//这只是一个例子
ViewData["QProducts"] = from p in db.Products select new{Name = p.Name, Date = p.ToShortDateString() }
ViewData["QUsers"] = from u in db.Users select u;
我使用 foreach 循环来迭代 html 中的对象,如下所示:
foreach(var q in (IEnumerable)ViewData["QEvents"])
{
/*Print the data here*/
}
在使用 MVC 之前,我只使用了 asp:Repeater< /code>,但由于这是 MVC,我无法使用 ASP.NET 控件。
我该如何将这些数据传递给视图? 我真的没有在这里不使用匿名类型的选择。 <%#ViewData.Eval()%>
显然行不通。
有任何想法吗?
I've seen many questions about this, but i've never really got the answer that I need.
I'm converting a fairly large web application from Web Forms to MVC and after a while I encountred a problem with passing data to the view. In the Action I execute the code:
//This is just an example
ViewData["QProducts"] = from p in db.Products select new{Name = p.Name, Date = p.ToShortDateString() }
ViewData["QUsers"] = from u in db.Users select u;
I use a foreach loop to iterate over the objects in html, like this:
foreach(var q in (IEnumerable)ViewData["QEvents"])
{
/*Print the data here*/
}
Before using MVC I just used a asp:Repeater
, but since this is MVC I can't use ASP.NET controls.
How am I supposed to pass this data to the View? I don't really have the option of not using Anonymous Types here. <%#ViewData.Eval()%>
obviously won't work.
Any Ideas?
发布评论
评论(6)
不要使用匿名类型,而是创建一个类型来保存名称和日期:
然后在 Linq 查询中使用该类型:
将视图强式键入为
MyView>
,然后执行foreach(ViewData.Model 中的 var nameDate)...
Rather than an anonymous type, create a type to hold the name and date:
Then use that in your Linq query:
Strongly type your view to be
MyView<IEnumerable<NameDate>>
and then just do aforeach ( var nameDate in ViewData.Model )...
如果你觉得有点懒,你可以在这里使用这段代码...它有点长,但基本上它是反射的包装...
这是代码...
If you're feeling a little lazy, you can use this code here... It's a little long, but basically it's a wrapper for Reflection...
And here is the code...
考虑显式转换为列表并转换 ViewData:
您可以通过多种方式传递数据,如上面那样使用 ViewData,或使用 TempData 在 Action 之间传递。 您还可以使用 ViewData.Model 来包含强类型模型。 请注意,您必须将视图的定义更改为类似的内容
至于一个不错的转发器替换尝试 http:// /www.codeplex.com/MVCContrib。 他们有一个 Grid Html Helper 可能会有所帮助。
Consider explicitly converting to a list and casting the ViewData:
You can pass data in a few ways, using ViewData as you are above, or TempData to pass between Actions. You can also use ViewData.Model to contain a strongly typed model. Note that you will have to change the definition of the view to be something like
As for a nice repeater replacement try http://www.codeplex.com/MVCContrib. They have a Grid Html Helper that may help.
如果您想避免创建一个单独的类只是为了显示您的一个投影,您还可以求助于使用字典,如下所示:
然后您可以将视图页面键入到
最后迭代视图中的列表,如下所示:
不用说,缺点是您的代码现在充满了“魔术字符串”,由于缺乏编译时检查,因此更容易出错。
If you want to avoid creating a separate class just for displaying your one projection, you could also resort to using a dictionary, like so:
You can then type your viewpage to
And finally iterate over the list in the view like so:
Needless to say, the drawback is that your code is now rather full of "magic strings", making it more error prone because of the absence of compile time checking.
不能只使用 MVC 中的 RouteValueDictionary 吗?
Can't you just use the RouteValueDictionary from MVC?
我怀疑您正在寻找的是 ViewModel: http://msdn.microsoft.com /en-us/vs2010trainingcourse_aspnetmvc3fundamentals_topic7.aspx
I suspect that what you are looking for is a ViewModel: http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3fundamentals_topic7.aspx