I have two classes

public class Owner { [HiddenInput(DisplayValue = false)] public int Id { get; set; } [Display(Name = "Имя")] [Required] public string Name { get; set; } [Display(Name = "Почта")] [Required] public string Email { get; set; } public ICollection<HomeRepair> HomeRepairs { get; set; } public Owner() { HomeRepairs = new List<HomeRepair>(); } ... } public class HomeRepair { [HiddenInput(DisplayValue = false)] public int Id { get; set; } [Display(Name = "Адрес")] [Required] public string Address { get; set; } public int? OwnerId { get; set; } [Required] public Owner Owner { get; set; } ... } 

Controller method

  [HttpGet] public ActionResult Create() { SelectList owners = new SelectList(db.Owners, "Name", "Name"); ViewBag.Owners = owners; return View(); } [HttpPost] public ActionResult Create(HomeRepair homeRepair, Owner owner) { if (ModelState.IsValid) { homeRepair.RepairStatus = "Ремонт запланирован"; db.HomeRepairs.Add(homeRepair); foreach (var i in db.Owners) { if (i.Name == owner.Name) { i.HomeRepairs.Add(homeRepair); } } db.SaveChanges(); return RedirectToAction("Index"); } return View(); } 

Representation

 @model TestTask.Models.HomeRepair @using TestTask.Models; @{ ViewBag.Title = "Добавление дома"; } <h2>Добавление дома</h2> @using (Html.BeginForm()) { <h4>Дом</h4> <div class="editor-label"><b>@Html.DisplayNameFor(model => model.Address)</b></div> <div class="editor-field"> @Html.EditorFor(model => model.Address) @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" }) </div> <div class="editor-label"><b>@Html.DisplayNameFor(model => model.RepairStatus)</b></div> <div class="editor-field"> Ремонт запланирован </div> <h4>Владелец</h4> Owner owner = new Owner(); <div id="selectOwner" class="editor-label"><b>Выберите владельца</b></div> <div id="selectOwnerDropDownList" class="editor-field"> @Html.DropDownList("Name", ViewBag.Owners as SelectList) </div> <p> <a id="addNewOwner" class="addLink">Добавить нового владельца</a> </p> <div id="addOwner"> </div> <p> <input type="submit" value="Добавить" /> </p> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") <script> $('.addLink').one('click', function () { $("#selectOwner").hide(); $("#selectOwnerDropDownList").hide(); $("#addNewOwner").hide(); var htmlAdd = "<div class='editor-label'><b>Имя</b></div>" + "<div class='editor-field'>" + "<input type='text' name ='owner.Name' value=''>" + "<span class='field-validation-valid text-danger'" + "data-valmsg-for='owner.Name' data-valmsg-replace='true'></span>" + "</div>" + "<div class='editor-label'><b>Email</b></div>" + "<div class='editor-field'>" + "<input type='text' name='owner.Email' value=''>" + "<span class='field-validation-valid text-danger'" + "data-valmsg-for='owner.Email' data-valmsg-replace='true'></span>" + "</div>"; $('#addOwner').append(htmlAdd) }) </script> } 

Here is the form itself:

enter image description here

After clicking on "Add new owner":

enter image description here

And after clicking on the "Add" error pops up:

enter image description here

Validation of the fields HomeRepair works fine, but Owner does not. What could be the problem ?

  • Because the Owner field is optional, so if it is not specified, this is not considered an error. Let's rephrase the question. Let us return to what you want to enter for the data (you do not have any text explanation for the code) and then we will say that your code is not for this task, but for some other one. Look at my answer , maybe something will become clearer. - AK
  • You have one obvious mistake here, but if you start to understand how it should work, then you can answer the question for a very long time and it is not clear where to draw the line "the question is given an exhaustive answer." Specifically, this error in the screenshot comes from the fact that it is impossible to simply select and map to SelectList: you need the Text and Value fields, you can see the right direction in this question . In general, if there is no action on [Post] over this action (there are two of them), then there may be more mistakes. - AK

0