Hello. The task is as follows. There is a database of orders for inventory. The application contains the date, and, most importantly, the number of items required and a comment to each.
public class Request { public int id { get; set; } [Display(Name = "Дата")] public DateTime RequestDate { get; set; } public virtual ICollection<RequestContent> Content { get; set; } } This is the content of the application, here is the content of RequestContent:
public class RequestContent { [Key] public int ID { get; set; } public int RequestContentType { get; set; } public int Request { get; set; } public int Count { get; set; } public string Comment { get; set; } } The bottom line is that the database has a table with items that the user can, in principle, order. The problem is that I do not quite understand how in the Create New page to make the program receive from the database a list of objects that the user can order. Now the Create.cshrml file looks like this :
@model InventoryApp.Models.Request @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Request</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.RequestDate, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(?model => model.RequestDate, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.RequestDate, "", new { @class = "text-danger" }) </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> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } That is, the problem is that I do not quite understand how to transfer any data from the code to the view. There is a default model, but how to transfer something of your own is not very clear. Controller:
public class RequestsController : Controller { private RequestDBContext db = new RequestDBContext(); // GET: Requests public ActionResult Index() { return View(db.Requests.ToList()); } // GET: Requests/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Request request = db.Requests.Find(id); if (request == null) { return HttpNotFound(); } return View(request); } // GET: Requests/Create public ActionResult Create() { return View(); } // POST: Requests/Create // Чтобы защититься от атак чрезмерной передачи данных, включите определенные свойства, для которых следует установить привязку. Дополнительные // сведения см. в статье http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "id,RequestDate")] Request request) { if (ModelState.IsValid) { request.RequestDate = DateTime.Now; db.Requests.Add(request); db.SaveChanges(); return RedirectToAction("Index"); } return View(request); } // GET: Requests/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Request request = db.Requests.Find(id); if (request == null) { return HttpNotFound(); } return View(request); } // POST: Requests/Edit/5 // Чтобы защититься от атак чрезмерной передачи данных, включите определенные свойства, для которых следует установить привязку. Дополнительные // сведения см. в статье http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "id,RequestDate")] Request request) { if (ModelState.IsValid) { db.Entry(request).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(request); } // GET: Requests/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Request request = db.Requests.Find(id); if (request == null) { return HttpNotFound(); } return View(request); } // POST: Requests/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Request request = db.Requests.Find(id); db.Requests.Remove(request); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } Database Context:
public class RequestDBContext : DbContext { public DbSet<Request> Requests { get; set; } public DbSet<RequestContent> RequestsContent { get; set; } public DbSet<RequestType> RequestTypes { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } }