简体   繁体   English

通过仅列表(空列表)从强类型列表单元格获取单元格DisplayName

[英]Get cell DisplayName from strongly-typed-list cell by List only (empty list)

I have a list of "Book" (eg) as a model in view 我有“书” (例如)列表作为视图模型

@model List<Book>

I want to create a table that each column get it's header by Book's DisplayName propery: 我想创建一个表,该表的每一列均按Book的DisplayName属性获取其标题:

<tr>
        <th class="text-right">
            @Html.DisplayNameFor(modelItem => Model.someMetaData.lineNumber)
        </th>
        <th class="text-right">
            @Html.DisplayNameFor(modelItem => Model.someMetaData.FirstName)
        </th>
<tr>

You can use DisplayNameFor helper method when your model is of type IEnumerable<T> 当模型的类型为IEnumerable<T>时,可以使用DisplayNameFor helper方法。

@model IEnumerable<Book>
<table class="table table-responsive table-striped">
    <tr>
        <th>@Html.DisplayNameFor(a=>a.Id)</th>
        <th>@Html.DisplayNameFor(a => a.Name)</th>
    </tr>

@foreach (var b in Model)
{
    <tr>
        <td>@b.Id</td>
        <td>@b.Name</td>
    </tr>
}
</table>

This works only if your view is strongly typed to an IEnumerable<Book> , It will not work for List<Book> 仅当您的视图强类型IEnumerable<Book> ,此方法才有效;不适用于List<Book>

C# 强类型ConfigurationBuilder列表<object>从应用设置<div id="text_translate"><p>真的在为这个苦苦挣扎——它应该这么难吗?!</p><p> 我的 appsettings 中有一个简单的 object 数组:</p><pre> "AppSettings": { "Names": [ { "Id": "1", "Name": "Mike" }, { "Id": "2", "Name": "John" } ] }</pre><p> 然后我有一个 class</p><pre> public class AppSettings { public List&lt;Names&gt; Names { get; set; } = new List&lt;Names&gt;(); } public class Names { public string Id { get; set; } public string Name { get; set; } }</pre><p> 我在我的应用设置中读到:</p><pre> var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build(); var config = new AppSettings();</pre><p> 这就是出错的地方:</p><pre> config.Names = configuration.GetSection("AppSettings:Names") //&lt;&lt;&lt;&lt; what do I do here?</pre><p> 似乎它都与IConfigurationSection相关,这没有帮助。</p></div></object> - C# strongly typed ConfigurationBuilder List<object> from appsettings

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从强类型列表中填充DropDownlist - Populating a DropDownlist From a Strongly Typed List 将强类型列表添加到数据库 - Add Strongly Typed List to Database 强类型List.GroupBy() - Strongly Typed List.GroupBy() 将强类型列表公开? - Make Strongly Typed List Public? Linq强类型嵌套列表 - Linq strongly typed nested list 序列化强类型对象列表 - Serializing a list of strongly typed objects 强类型对象列表到动态列表 - List of Strongly typed objects to list of dynamic 从视图返回填充列表的强类型列表? - Strongly typed list return from View to fill Model? 似乎无法从数组反序列化为强类型列表对象 - cant seem to Deserialize to strongly typed list object from array C# 强类型ConfigurationBuilder列表<object>从应用设置<div id="text_translate"><p>真的在为这个苦苦挣扎——它应该这么难吗?!</p><p> 我的 appsettings 中有一个简单的 object 数组:</p><pre> "AppSettings": { "Names": [ { "Id": "1", "Name": "Mike" }, { "Id": "2", "Name": "John" } ] }</pre><p> 然后我有一个 class</p><pre> public class AppSettings { public List&lt;Names&gt; Names { get; set; } = new List&lt;Names&gt;(); } public class Names { public string Id { get; set; } public string Name { get; set; } }</pre><p> 我在我的应用设置中读到:</p><pre> var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build(); var config = new AppSettings();</pre><p> 这就是出错的地方:</p><pre> config.Names = configuration.GetSection("AppSettings:Names") //&lt;&lt;&lt;&lt; what do I do here?</pre><p> 似乎它都与IConfigurationSection相关,这没有帮助。</p></div></object> - C# strongly typed ConfigurationBuilder List<object> from appsettings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM