There are two one-to-many models
Bid model
public class Bid { public int Id { get; set; } public string Name { get; set; } public virtual ICollection Documents { get; set; } } Document Model
public class Document { public int Id { get; set; } public string Name { get; set; } public string Url { get; set; } public int? BidId { get; set; } public virtual Bid Bid { get; set; } } I need to implement a page with the addition of the application (Bid), on which it is possible to add documents right away, for example, using a modal bootstrap (pop-up window).
Tell me, please, how can I implement a controller that could add documents to an application that has not yet been added.
PS I can implement this thing using jQuery, which generates the markup, and the model binder, but this approach is poorly followed and difficult to test. There must be another way :)
What I do:
I create EditorTemplates for Bid and Document
Bid
@model Intrasite.Domain.Entities.Bid @Html.HiddenFor(model=>model.Id) @Html.LabelFor(model=>model.Name) @Html.EditorFor(model=>model.Name) @Html.EditorFor(model=>model.Documents) Document @model Intrasite.Domain.Entities.Document @Html.HiddenFor(model => model.Id) @Html.LabelFor(model => model.Name) @Html.EditorFor(model => model.Name) I make a View to create an application:
@using (Html.BeginForm("Create", "Home")) { @Html.EditorForModel() a href="" data-toggle="modal" data-target="#myModal">Добавить документ /a div class="modal fade" id="myModal" ... @Html.EditorFor(model => model.Documents) ... /div input type="submit" value="Создать заявку" / } And then the problems begin. I cannot add separately, through separate methods, documents to the application, since it has not been added yet. I have a controller with a method
public ActionResult Create() { var bid = new Bid() { Documents = new List() {new Document()} }; return View(bid); } [HttpPost] public ActionResult Create(Bid bid) { if (!ModelState.IsValid) return View(bid); repository.CreateBid(bid); return View(bid); } Since I submit the application with the created document Documents = new List() {new Document()} , accordingly, I can fill only one document. I also created a method
[HttpPost] public ActionResult CreateDocument(Bid bid) { bid.Documents.Add(new Document()); return View("Create", bid); } Which allowed me to add a lot of documents, but everything is crooked.
I want to know how smart people still implement such connections