开发者

ViewData.Eval for Collections

I believe that ViewData.Eval does not work for collection objects.

The following returns null.

<%=ViewData.Eval("Names[0].FirstName")%>
<%=ViewData.Eval("Names[1].FirstName")%>

My app requires that the model be accessed using Eval and not strongly typed methods like Model.Names[0].FirstName etc.

How can I achieve this?

Ver开发者_运维问答sion of MVC is 1.0

Thanks.


I was looking at this problem when creating custom Html helper methods for rendering controls. I wanted to be able to get the view data model value generically given an expression. The standard ViewData.Eval method works fine for properties, but didn't work at all for more complex cases. I ended up using the DataBinder class as per the example below:

var value = DataBinder.Eval(ViewData.Model, "Names[0].FirstName")

This seemed to solve my problem.


This is how I achieve it.

I first assign individual array item to ViewData and then use ViewData's Eval method.

<%
ViewData["SingleNameObject"] = Names[i];
%>

<%=ViewData.Eval("SingleNameObject.FirstName")%>

Thanks.


<%=ViewData.Eval("Names.0.FirstName")%>
<%=ViewData.Eval("Names.1.FirstName")%>

ASP.NET MVC ViewData.Eval() method - IBloggable - implemented

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜