在我的视图中无法访问viewmodel中的元数据

时间:2017-07-05 12:20:16

标签: c# asp.net-mvc

我想在我的视图中使用元数据,但我的模型(ToDoTable)打包在viewmodel(ModelsViewList)中,我不知道如何到达模型。@ foreach(ViewData.ModelMetadata.Properties中的var属性)不起作用:(。

型号:

public partial class ToDoTable
        {
            [HiddenInput(DisplayValue = false)]
            public int ID { get; set; }

            [Required(ErrorMessage = "Please write name of event.")]
            public string EventName { get; set; }



            [MaxLength(50, ErrorMessage = "Description must be shorter than 50 characters ")]
            [Required(ErrorMessage = "Proszę podać opis.")]
            public string Description { get; set; }


            [Display(Name = "Tu wpisz format daty")]
            [DataType(DataType.Date, ErrorMessage = "Date mast be in format described")]
            public string Data { get; set; }
        } 

控制器:

 using System.Web.Mvc;
        using ToDoLibrary.Abstract;
        using ToDoUI.ModelsViews;

        namespace ToDoUI.Controllers
        {
            public class HomeController : Controller
            {
                IToDoRepository repository;
               public HomeController(IToDoRepository rep)
               {
                    repository = rep;
               }
                public ViewResult List()
                {
                    ToDoLibrary.ToDoTable model = new ToDoLibrary.ToDoTable();
                    ModelsViewList modelView = new ModelsViewList()
                    {
                        FromRepository = repository.FromRepository,
                        ModelDB = model
                    };
                   return View(modelView);
                }

           }`
       }

查看:

@model ToDoUI.ModelsViews.ModelsViewList
    @{
        ViewBag.Title = "List";
    }
    <div class="row">
        <div class="col-lg-6 main tile1 col-lg-push-6">
            pierwsza kolumna

            @using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <div class="panel-body">

                    @foreach (var property in ViewData.ModelMetadata.Properties)
                    {
                        switch (property.PropertyName)
                        {
                            case "ID":


                                break;
                            default:
                                <div class="form-group">
                                    <label>@(property.DisplayName ?? property.PropertyName)</label>
                                    @if (property.PropertyName == "Description")
                                {
                                        @Html.TextArea(property.PropertyName, null, new { @class = "form-control", rows = 5 })
                                    }
                                    else
                                    {
                                        @Html.TextBox(property.PropertyName, null, new { @class = "form-control" })
                                    }
                                    @Html.ValidationMessage(property.PropertyName)
                                </div>
                                break;
                        }
                    }
                </div>
                <div class="panel-footer">
                    <input type="submit" value="Zapisz" class="btn btn-primary" />
                    @Html.ActionLink("Anuluj i wróć do listy", "Index", null, new
               {
                   @class = "btn btn-default"
               })
                </div>
            }
         </div>
    </div>

2 个答案:

答案 0 :(得分:1)

您的属性名称应该更像ModelDB.ID,因为您的元数据与ToDoUI.ModelsViews.ModelsViewList相关,其中包含属性ModelDB,这是您引用的ToDoTable对象。< / p>

否则:

var metaData = ModelMetadataProviders.
     Current.GetMetadataForType(null, Model.ModelDB.GetType()); 

这将为您提供TodoTable的元数据,然后您可以浏览您的属性。无论如何,我不确定它会帮助你做一些体面的......因为它看起来不会像:)

答案 1 :(得分:1)

你做不到。但是,问题不在于如何从实体类中获取元数据,而是在实体类中首先不应该具有此元数据。这些注释属于您的视图模型。实体类应该只包含对数据库有用的东西,而不是你的视图。