adding additional fields to EF dataset in View and submitting RRS feed

  • Question

  • User-173333858 posted

    I am new to MVC, EF. My MVC project has a view that displays data that I get via EF. So, that model subset is already created. But after the user sees this form, I need to add an additional field 'COMMENTS' to his form, and have the user submit it. I created a separate Model for this 'comments' field, but am unable to include more than 1 model in this view page, something like this (which will look as a huge blunder to people who know this well, I am not one of them yet :) )

    @model IEnumerable<Admin_Tool.Func_EmpDetails_Result>
    @model Admin_Tool.Models.Form
    I think the best way would be to create another Model Class with these 2. Let me know if this is correct? Or is there a better way.
    Tuesday, March 16, 2021 1:48 PM

All replies

  • User475983607 posted

    The openly standard solution is crafting a complex model which is simply a model that has other model types as properties.  This concept is covered in most if not all beginning level MVC and Entity Framework tutorials.  In MVC it is called a View Model.  In EF it is an entity with navigation properties.

    If all you need is another column then simply add the property by following the reference documentation for the data approach you are using; Code first or DB first.

    Tuesday, March 16, 2021 1:52 PM
  • User1120430333 posted

    There is no such thing as an EF dataset. EF is an ORM using a conceptual virtual model with entities.

    LINQ to Entities - ADO.NET | Microsoft Docs

    If you where using a viewmodel, then there should be no need to make another model. You should be able to just add the Comment property to the viewmodel that is not the EF persistence model being used as a viewmodel, becuase they are two models with different purposes.

    Understanding ViewModel in ASP.NET MVC (dotnettricks.com)

    Kinds of Models | DevIQ

    Tuesday, March 16, 2021 7:29 PM