ASP.NET MVC。通过 viewData 传递列表
大家好,有谁知道如何通过“ViewData”传递列表。这就是我正在尝试的,但我想我在某些地方缺少演员。
List<GalleryModel> galleryList = new List<GalleryModel>();
galleryList.Add(new GalleryModel() { isApproved = true, uri = "www.cnn1.com" });
galleryList.Add(new GalleryModel() { isApproved = true, uri = "www.cnn2.com" });
ViewData["SomeList"] = galleryList;
这是我的 aspx 页面代码:
<% List<myNS.CM.AVDTalentApplication.Models.GalleryModel> galList = ViewData["SomeList"]; %>
<% foreach (var gal in galList) { %>
<%= gal.uri%>
<%} %>
Hi does anyone know how to pass a list throught the "ViewData". This is what I'm trying but I think I'm missing a cast some where.
List<GalleryModel> galleryList = new List<GalleryModel>();
galleryList.Add(new GalleryModel() { isApproved = true, uri = "www.cnn1.com" });
galleryList.Add(new GalleryModel() { isApproved = true, uri = "www.cnn2.com" });
ViewData["SomeList"] = galleryList;
here's my aspx page code:
<% List<myNS.CM.AVDTalentApplication.Models.GalleryModel> galList = ViewData["SomeList"]; %>
<% foreach (var gal in galList) { %>
<%= gal.uri%>
<%} %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于这一行:
将其更改为
For this line:
change it to
您需要将其投射到视图中:
或
You need to cast it in the view:
or
您必须将对象从 ViewData 集合中显式转换为您需要与之交互的类型:
You have to explicitly cast the object out of the ViewData collection as the type you need to interact with:
即使上述所有答案都是正确的,我强烈建议使用 查看模型。
Even though all the above answers are correct, I would strongly suggest making use of view models.
只需将此代码添加到控制器:
ViewData("SomeRole") = "hidden"/"visible"
到您的 html
Just Add this code to controller:
ViewData("SomeRole") = "hidden"/"visible"
To your html <li style="visibility:
@ViewBag.SomeRole'>@Html.ActionLink("Tenant", "Index", "{controller}", New With {.area = ""}, Nothing)