The following entities are defined in NHibernate:
public class Nomenclature { public virtual int NomenclatureId { get; set; } public virtual NomenclatureType NomenclatureType { get; set; } public virtual IDictionary<NomenclatureAttribute, string> Attributes { get; set; } } public class NomenclatureType { public virtual int NomenclatureTypeId { get; set; } public virtual string Name { get; set; } public virtual ICollection<Nomenclature> Nomenclatures { get; set; } public virtual ICollection<NomenclatureAttribute> NomenclatureAttributes { get; set; } public NomenclatureType() { Nomenclatures = new HashSet<Nomenclature>(); NomenclatureAttributes = new HashSet<NomenclatureAttribute>(); } } public class NomenclatureAttribute { public virtual int NomenclatureAttributeId { get; set; } public virtual string AttributeName { get; set; } public virtual string AttributeType { get; set; } public virtual NomenclatureType NomenclatureType { get; set; } } They represent the description of the item model in the application. Describing the entity Nomenclature made the Controller and added Create there
[HttpGet] public ActionResult Create(string nomenclatureType) { if (nomenclatureType == null) return RedirectToAction("List", "Nomenclature"); ViewData["NomenclatureAttributes"] = _repositoryNomenclatureType.Get(w => w.Name == nomenclatureType).NomenclatureAttributes.ToList(); return View(); } [HttpPost] public IActionResult Create(Nomenclature nomenclature) { try { if (ModelState.IsValid) { _repositoryNomenclature.Create(nomenclature); return RedirectToAction("List", "Nomenclature"); } } catch (Exception) { ModelState.AddModelError("", "Unable to save changes."); } return View(nomenclature); } In the view I look through all the attributes of the item, but so far I have only managed to create a label for all the available attributes of the item type transferred to the controller. How do I use the model binding mechanism (or something else) to create an editor for each attribute and write it into Model.Attributes. Where Attributes is public virtual IDictionary<NomenclatureAttribute, string> Attributes { get; set; } public virtual IDictionary<NomenclatureAttribute, string> Attributes { get; set; }
@model Nomenclature @{ ViewBag.Title = "New nomenclature"; Layout = "_Layout"; } @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> @Html.ValidationSummary(true) @foreach (var a in (List<NomenclatureAttribute>)ViewData["NomenclatureAttributes"]) { <div class="form-group"> <label class="control-label col-md-2">@a.AttributeName</label> <div class="col-md-10"> **Код эдитора которого нет** </div> </div> } <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } I use Asp.net core web application (.NET Framework)