I have a .net MVC page that has a form used to edit an entity from database. The form has some dynamic select list fields that need to be populated from Database also.
I pass a PageView Model that contains the Data for the entity, and a list of entities to populate the select list like so:
public class EditPortalPage
{
public PortalViewModel Data { get; set; }
public IEnumerable<SelectListItem> Cultures { get; set; }
}
in my View I have
@model Website.Models.Portals.EditPortalPage
at the top of the page
I can easily populate my form doing this:
@Html.EditorFor(model => model.Data.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.LabelFor(model => model.Data.Name)
@Html.ValidationMessageFor(model => model.Data.Name, "", new { @class = "text-danger" })
@Html.DropDownListFor(model => model.Data.DefaultCultureId, new SelectList(Model.Cultures, "Value", "Text"), new { @class = "mdb-select colorful-select dropdown-info" })
@Html.LabelFor(model => model.Data.DefaultCultureId)
@Html.ValidationMessageFor(model => model.Data.DefaultCultureId, "", new { @class = "text-danger" })
The problem is this produces messed up Id and Name attributes for my form fields. In the above example it produces:
Name Field ID: Data_Name
Name Field Name: Data.Name
What is the correct way to set it up so I the name and ID does not include the 'Data' prefix
Can I pass the Data model directly to teh form?