mvc5 asp.net 显示数据问题

wsy199126321 2016-12-16 10:41:23
1.view
@foreach (var item in Model)//行
{
<tr>
@foreach (var member in ViewBag.列名集)//列
{
<td align="center">@item.@member</td>
//此处想显示表中数据,*表名.列名*,但是列名是变量member,请问如何写?

@*<td align="center">@Html.DisplayFor(modelItem => item.GetHashCode(member))</td>*@
}
</tr>
}

2.Controller
var ItmesA = mdb.T_表示列名.Select(c => c.列名 ).ToList();

ViewBag.列名集 = ItmesA;

return View(mdb.T_Mスマ履歴.ToList());
...全文
159 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
用反射 @item.GetType().GetProperty(member).GetValue(item) 但是没看出来这么做有什么意思,只为了视图层显示指定的属性吗?ViewModel干嘛去了? 甚至你定义个Model<T, T_表示列名>这样的基类也行啊 再用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)将T(行数据)转成IDictionary<string, object>,有了属性字典和列名集,不难办了吧?
wsy199126321 2016-12-16
  • 打赏
  • 举报
回复
引用 2 楼 xiaojie_cp 的回复:
可以考虑使用强类型。。。。。
是对Member吗,能详细点吗
wsy199126321 2016-12-16
  • 打赏
  • 举报
回复
@item.@member 这样写我试了,显示的时候是表名.列名,而不是数据 MSmaBrowser.Models.T_Mスマ履歴.施工種別-----显示的是这个 而不是*施工種別*列的数据。
lovebaby 2016-12-16
  • 打赏
  • 举报
回复
可以考虑使用强类型。。。。。
EdsionWang 2016-12-16
  • 打赏
  • 举报
回复

     <td align="center">@item.@member</td>
就这样写呗
wsy199126321 2016-12-16
  • 打赏
  • 举报
回复
引用 5 楼 dongxinxi 的回复:
用反射 @item.GetType().GetProperty(member).GetValue(item) 但是没看出来这么做有什么意思,只为了视图层显示指定的属性吗?ViewModel干嘛去了? 甚至你定义个Model<T, T_表示列名>这样的基类也行啊 再用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)将T(行数据)转成IDictionary<string, object>,有了属性字典和列名集,不难办了吧?
我还有一个小问题想请教您。 列名里包含括号(),item点出来就变成下划线了。 @item._姓名_ 表里是:(姓名) 这样@item.GetType().GetProperty(member).GetValue(item) 取出来的是(姓名),如何变成_姓名_啊? 万分感谢
wsy199126321 2016-12-16
  • 打赏
  • 举报
回复
引用 6 楼 wsy199126321 的回复:
[quote=引用 5 楼 dongxinxi 的回复:] 用反射 @item.GetType().GetProperty(member).GetValue(item) 但是没看出来这么做有什么意思,只为了视图层显示指定的属性吗?ViewModel干嘛去了? 甚至你定义个Model<T, T_表示列名>这样的基类也行啊 再用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)将T(行数据)转成IDictionary<string, object>,有了属性字典和列名集,不难办了吧?
我只是想在页面显示数据 有两个表。T_表示列名,里面有个列叫‘列名’。里面存的是需要显示另一表的列名。 我先试试您说的[/quote] 成功啦~~~谢谢您哈,大神啊~~~
wsy199126321 2016-12-16
  • 打赏
  • 举报
回复
引用 5 楼 dongxinxi 的回复:
用反射 @item.GetType().GetProperty(member).GetValue(item) 但是没看出来这么做有什么意思,只为了视图层显示指定的属性吗?ViewModel干嘛去了? 甚至你定义个Model<T, T_表示列名>这样的基类也行啊 再用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)将T(行数据)转成IDictionary<string, object>,有了属性字典和列名集,不难办了吧?
我只是想在页面显示数据 有两个表。T_表示列名,里面有个列叫‘列名’。里面存的是需要显示另一表的列名。 我先试试您说的
An outstanding author team presents the ultimate Wrox guide to ASP.NET MVC 4 Microsoft insiders join giants of the software development community to offer this in-depth guide to ASP.NET MVC, an essential web development technology. Experienced .NET and ASP.NET developers will find all the important information they need to build dynamic, data-driven websites with ASP.NET and the newest release of Microsoft's Model-View-Controller technology. Featuring step-by-step guidance and lots of code samples, this guide gets you started and moves all the way to advanced topics, using plenty of examples. Designed to give experienced .NET and ASP.NET programmers everything needed to work with the newest version of MVC technology Expert author team includes Microsoft ASP.NET MVC insiders as well as leaders of the programming community Covers controllers, views, models, forms and HTML helpers, data annotation and validation, membership, authorization, security, and routing Includes essential topics such as Ajax and jQuery, NuGet, dependency injection, unit testing, extending MVC, and Razor Includes additional real-world coverage requested by readers of the previous edition as well as a new case study example chapter 下面是上一版书的中文介绍 《ASP.NET MVC 3 高级编程》主要内容是描述视图的概念,探讨Razor语法、NuGet、单元测试等,解释控制器在MVC框架中的作用,以及模型在绑定和数据访问策略中发挥的作用,演示如何显示和处理表单,涵盖添加到April 2011 Tools Update中的新特性,如基架和HTML5项目模板,展示在客户端和服务器端验证模型的过程给出利用成员资格、援权和安全特性的忠告,讲解将TDD应用于ASP.NET MVC的方法。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧