第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在剃刀視圖引擎中獲取集合中項(xiàng)目的元數(shù)據(jù)?

如何在剃刀視圖引擎中獲取集合中項(xiàng)目的元數(shù)據(jù)?

C#
SMILET 2021-11-14 15:52:21
我有一個(gè)項(xiàng)目寫在C#ASP.NET MVC 5 框架的頂部。我正在嘗試將我的視圖與視圖模型分離,以便我可以重用我的視圖。隨著大量使用,EditorTemplates我能夠通過評(píng)估ModelMetadata模型上每個(gè)屬性的和 數(shù)據(jù)注釋屬性來創(chuàng)建我的所有標(biāo)準(zhǔn)視圖(即創(chuàng)建、編輯和詳細(xì)信息),然后呈現(xiàn)頁面。我唯一感到困惑的是如何渲染Index視圖。我的索引視圖通常接受一個(gè)IEnumerable<object>或IPagedList<object>集合。在我看來,我希望能夠評(píng)估集合中每個(gè)object/record的 ModelMetadata以確定是否object應(yīng)該顯示該屬性。換句話說,我的視圖模型看起來像這樣public class DisplayPersonViewModel{    public int Id{ get; set; }    [ShowOnIndexView]    public string FirstName { get; set; }    [ShowOnIndexView]    public string LastName { get; set; }    [ShowOnIndexView]    public int? Age { get; set; }    public string Gender { get; set; }}然后我的Index.cshtml視圖將接受IPagedList<DisplayPersonViewModel>集合中的每條記錄,我想顯示用ShowOnIndexView屬性修飾的屬性的值。通常,我可以使用這樣的方法來評(píng)估我認(rèn)為的 ModelMetadata@model IPagedList<object>@{var elements = ViewData.ModelMetadata.Properties.Where(metadata => !metadata.IsComplexType && !ViewData.TemplateInfo.Visited(metadata))                                                .OrderBy(x => x.Order)                                                .ToList();}<tr>    @foreach(var element in elements)    {        var onIndex = element.ContainerType.GetProperty(element.PropertyName)                             .GetCustomAttributes(typeof(ShowOnIndexView), true)                             .Select(x => x as ShowOnIndexView)                             .FirstOrDefault(x => x != null);        if(onIndex == null)         {            continue;        }        @Html.Editor(element.PropertyName, "ReadOnly")    }</tr>然后我的控制器看起來像這樣public class PersonController : Controller{        public ActionResult Index()        {            // This would be a call to a service to give me a collection with items. but I am but showing the I would pass a collection to my view            var viewModel = new List<DisplayPersonViewModel>();            return View(viewModel);        }}然而與評(píng)估問題ModelMetadata的IPagedList<DisplayPersonViewModel>是,它給了我關(guān)于集合本身不是泛型類型或集合中的單一模式的信息。換句話說,我得到了諸如總頁數(shù)、每頁項(xiàng)目數(shù)、總記錄數(shù)之類的信息......題如何訪問ModelMetadata集合中每一行的信息,以便能夠知道要顯示哪些屬性,不顯示哪些屬性?
查看完整描述

1 回答

?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊

我將通過建議您不要使用此類代碼污染您的視圖來介紹此答案。更好的解決方案是創(chuàng)建一個(gè)自定義HtmlHelper擴(kuò)展方法來生成 html,這為您提供了更大的靈活性,可以進(jìn)行單元測(cè)試,并且可以重用。


您需要更改的第一件事是視圖中的模型聲明


@model object

否則你會(huì)拋出這個(gè)異常(List<DisplayPersonViewModel>不是IEnumerable<object>或IPagedList<object>,但它是object)


需要注意的是,目前尚不清楚,如果你想ModelMetadata為type在集合或集合中的每個(gè)項(xiàng)目,所以我既包括,加代碼,獲取type


@model object

@{

    var elements = ViewData.ModelMetadata.Properties.Where(metadata => !metadata.IsComplexType && !ViewData.TemplateInfo.Visited(metadata)).OrderBy(x => x.Order).ToList();


    // Get the metadata for the model

    var collectionMetaData = ViewData.ModelMetadata;

    // Get the collection type

    Type type = collectionMetaData.Model.GetType();

    // Validate its a collection and get the type in the collection

    if (type.IsGenericType)

    {

        type = type.GetInterfaces().Where(t => t.IsGenericType)

            .Where(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>))

            .Single().GetGenericArguments()[0];

    }

    else if (type.IsArray)

    {

        type = type.GetElementType();

    }

    else

    {

        // its not a valid model

    }

    // Get the metadata for the type in the collection

    ModelMetadata typeMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, type);

}

....

@foreach (var element in collectionMetaData.Model as IEnumerable))

{

    // Get the metadata for the element

    ModelMetadata elementMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => element, type);

    ....

請(qǐng)注意,使用反射來確定循環(huán)的每次迭代中是否存在該屬性是低效的,我建議您這樣做一次(基于typeMetadata)。然后您可以(例如)生成一個(gè)bool值數(shù)組,然后在循環(huán)中使用索引器來檢查值)。更好的選擇是讓您的ShowOnIndexView屬性實(shí)現(xiàn)IMetadataAware并向AdditionalValuesof添加一個(gè)值,ModelMetadata以便var onIndex根本不需要代碼。有關(guān)實(shí)現(xiàn)的示例IMetadataAware,請(qǐng)參閱CustomAttribute 反映 html 屬性 MVC5


查看完整回答
反對(duì) 回復(fù) 2021-11-14
  • 1 回答
  • 0 關(guān)注
  • 176 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)