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>(); } } 
  • What is the point to insert into the question links to the code on another resource? Paste all the necessary code right here. - tym32167
  • And the essence of the question is not clear You do not know how to contact the database? Or what does not work? - tym32167
  • I cannot insert the Create.cshtml code because it is incorrectly displayed here. The essence of the question is that yes, I do not know how to turn to the database, where to do it, and how to get a list of available items in the Create.cshtml file. - Grigory Kondrashov
  • You use ASP.NET MVC, that is, Model-View-Controller. You showed the Model and View - It remains to add the Controller and contact the database there. Read more here - tym32167
  • The problem is that I do not know how to turn to the database and where to do it. That is, how to take data from somewhere in the page layout file. - Grigory Kondrashov

1 answer 1

Transfer data to a View via ViewBag or ViewData

 // GET: Requests public ActionResult Index() { ViewBag.Name = "MyName"; //или ViewData["Name"] = "MyName"; return View(db.Requests.ToList()); } 

Create.cshrml

 <span>@ViewBag.Name</span> <span>@ViewData["Name"]</span> 

The second way to make a custom ViewModel with the required fields.

 public class MyViewModel { public string Name; public Request Request; //... } 

and then

 // GET: Requests public ActionResult Index() { return View(new MyViewModel { Request = db.Requests.ToList(), Name = "MyName", }); }