asp.net-mvc-3 – 动态地为每个列的ASP.NET MVC Razor标头和视图
发布时间:2020-12-16 07:17:06 所属栏目:asp.Net 来源:网络整理
导读:我现在有以下Razor线: table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box"tr thTürk?e S?z Dizisi/th thEnglish Word Sequence/th th/th/tr@foreach (var item in Model) {tr td @Html.DisplayFor(modelItem = item.Tr) /td td @Html.D
我现在有以下Razor线:
<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box"> <tr> <th>Türk?e S?z Dizisi</th> <th>English Word Sequence</th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Tr) </td> <td> @Html.DisplayFor(modelItem => item.En) </td> <td> @Html.ActionLink((String)@ViewBag.Edit,"Edit",new { id=item.ID }) | @Html.ActionLink((String)@ViewBag.Delete,"Delete",new { id=item.ID }) </td> </tr> } </table> 但是,我会自动从程序到数据库表添加一些列.我需要一种方法来打印这个和td相应的.简而言之,我需要一种方法来浏览Model的动态列.我怎样才能做到这一点? 编辑:我的模型类型是“提案”.但是,我想要达到Proposal.Type.Word(Tr,En或任何其他添加的语言枚举)的动态属性.我怎么能够? @foreach (var item in Model) { Type objectType = Type.GetType(typeof(RexamOneriSistemi.Models.Word).AssemblyQualifiedName); System.Reflection.PropertyInfo[] fields = objectType.GetProperties(); <tr> <td>@Html.DisplayFor(modelItem => item.ID)</td> <td>@Html.DisplayFor(modelItem => item.Owner.Name)</td> @foreach (System.Reflection.PropertyInfo f in fields) { if (f.Name.ToString() == @HttpContext.Current.Session["Language"].ToString()) { <td> // What to do here exactly to get item.Type.Word.[dynamic attribute]? </td> } </tr> } 我可以得到Razor String string s = "@Html.Displayfor(modelItem => item.Type.Word." + System.Web.HttpContext.Current.Session["Language"] + ")" Resulting: @Html.Displayfor(modelItem => item.Type.Word.Tr) 我可以插入要呈现为Razor语法的字符串吗?如果有,怎么样? 解决方法
我试过这个代码,但它确实有效
<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box"> <tr> @{ Type objectType = Type.GetType(typeof(yourProjectNamespace.Models.Language).AssemblyQualifiedName); System.Reflection.PropertyInfo[] fields = objectType.GetProperties(); foreach (System.Reflection.PropertyInfo f in fields) { <th> @f.Name.ToString() </th> } } </tr> @foreach (yourModelType item in Model) { <tr> foreach (System.Reflection.PropertyInfo f in fields) { <td>@Html.Display(f.Name.ToString())</td> } <td> @Html.ActionLink((String)@ViewBag.Edit,new { id=item.ID }) | @Html.ActionLink((String)@ViewBag.Delete,new { id=item.ID }) </td> </tr> } </table> 要获得课程的Assemply Qualified Name,请参阅此链接here (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在foreach中的mvc radiobuttons
- asp.net – 调用RenderSection两次?
- asp.net – 如何为我的网站实现Facebook“赞”按钮
- asp.net-mvc – ASP.NET MVC自定义路由搜索
- 创建代码生成器可以很简单:如何通过T4模板生成代码?[上篇
- asp.net – 如何使用JQuery与母版页?
- asp.net-mvc-3 – MVC配置授权角色值和强类型角色
- asp.net core 从单机到集群
- 全面理解 ASP.NET Core 依赖注入
- asp.net-web-api – 角色提供者/成员?如何在asp.net web a
推荐文章
站长推荐
- asp.net-core-mvc – 什么是Asp.Net Core MVC中的
- asp.net-mvc-2 – Plus()在MVC中的参数引起404在
- 调试 ASP.NET Core 2.0 源代码
- 在ASP.NET中的Response.Redirect中传递cookie
- asp.net – MVC 4如何为WebGrid设置行ID
- asp.net – “localhost”上运行网站是非常慢
- kendo-ui – Kendo UI网格过滤器无法在引导程序M
- asp.net-mvc-2 – 将模型数据传递给外部javascri
- asp.net – 服务应用程序池“X”的进程与Windows
- asp.net-mvc – Razor中()(括号)和{}(大括号)之间
热点阅读