Marek
Marek

Reputation: 111

Can't reach metadata in viewmodel in my view

I would like to use metadata in my view but my model(ToDoTable) is packed in viewmodel(ModelsViewList) and I don't know how to reach the model.The @foreach (var property in ViewData.ModelMetadata.Properties) doesn't work :(.

Model:

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; }
        } 

Controller:

 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);
                }

           }`
       }

View:

@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>

Upvotes: 0

Views: 275

Answers (2)

Laurent Lequenne
Laurent Lequenne

Reputation: 902

Your property name should be more like ModelDB.ID as your metadata is related to ToDoUI.ModelsViews.ModelsViewList which contains a property ModelDB, which is your ToDoTable object you are referencing.

Otherwise :

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

This will give you the metadata of your TodoTable you can then go through your properties. Anyway I am not sure it will help you to do something decent... As it doesn't look like it will :)

Upvotes: 1

Chris Pratt
Chris Pratt

Reputation: 239300

You can't. However, the problem is not one of how to get the metadata from the entity class, but rather instead, that the entity class should not have this metadata in the first place. These annotations belong on your view model. The entity class should only have things that matter to the database, not your view.

Upvotes: 1

Related Questions