Я относительно новичок в MVC 5, и у меня возникла проблема с выяснением того, как обновить реляционную таблицу.Сначала я использую код EF.Я потратил много времени на поиск решения, но я просто не могу найти ничего, что относится к моему коду, поэтому у меня нет другого выбора, кроме как спросить здесь, поэтому я прошу прощения заранее, если на это уже был дан ответ.
Я включил реляционную таблицу в мое представление Razor, которое работает очень хорошо.Но когда я хочу обновить таблицу, она просто не обновляется, даже если я не получаю ошибок с моим текущим кодом.Я также попытался добавить дополнительную привязку [(Include = "Id, FirstName, LastName")], но при этом использовались Booking FirstName и LastName.Я не уверен, являются ли мои методы «правильными».Моя логика говорит мне, что мне нужно извлечь данные из представления и извлечь их в контроллере, но я не знаю, как, если я не могу использовать Bind или db.Entry (item) .State = EntityState.Modified?
Booking.cs
namespace Ventures.Innovation.InteractiveModel.Context
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Bookings
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Bookings()
{
AttendingPeople = new HashSet<AttendingPeople>();
}
public int Id { get; set; }
[Required]
[StringLength(128)]
public string AspNetUserFk { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
[StringLength(100)]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
public string LastName { get; set; }
public virtual AspNetUsers AspNetUsers { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AttendingPeople> AttendingPeople { get; set; }
}
}
AttendingPeople.cs
namespace Ventures.Innovation.InteractiveModel.Context
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class AttendingPeople
{
public int Id { get; set; }
public int BookingId { get; set; }
[Required]
[StringLength(100)]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
public string LastName { get; set; }
public virtual Bookings Bookings { get; set; }
}
}
Контроллер
// GET: AdminHome/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Bookings bookings = db.Bookings.Find(id);
if (bookings == null)
{
return HttpNotFound();
}
ViewBag.AspNetUserFk = new SelectList(db.AspNetUsers, "Id", "Email", bookings.AspNetUserFk);
var attendingPeople = db.AttendingPeople.Where(a => a.BookingId == id).ToList();
bookings.AttendingPeople = attendingPeople;
return View(bookings);
}
// POST: AdminHome/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,AspNetUserFk,Date,FirstName,LastName")] Bookings bookings)
{
if (ModelState.IsValid)
{
db.Entry(bookings).State = EntityState.Modified;
db.SaveChanges();
var editedAttendingPeople = db.AttendingPeople.Where(a => a.BookingId == bookings.Id).ToList();
foreach (var item in editedAttendingPeople)
{
db.Entry(item).State = EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AspNetUserFk = new SelectList(db.AspNetUsers, "Id", "Email", bookings.AspNetUserFk);
return View(bookings);
}
Просмотр
@model Ventures.Innovation.InteractiveModel.Context.Bookings
@{
ViewBag.Title = "Edit booking";
}
<h2>Edit booking</h2>
<hr />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
</div>
<div class="form-group">
@Html.LabelFor(model => model.AspNetUserFk, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AspNetUserFk", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AspNetUserFk, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
@if (Model.AttendingPeople.Count > 0)
{
@Html.Label("Attending people")
foreach (var attendingPeople in Model.AttendingPeople)
{
<div class="form-group">
<div class="col-md-10">
@Html.LabelFor(modelItem => attendingPeople.FirstName, htmlAttributes: new { @class = "control-label col-md-5", @style = "padding-left: 0;" })
@Html.LabelFor(modelItem => attendingPeople.LastName, htmlAttributes: new { @class = "control-label col-md-5", @style = "padding-left: 0;" })
</div>
<div class="col-md-10">
@Html.EditorFor(modelItem => attendingPeople.FirstName, new { htmlAttributes = new { @class = "form-control col-md-5", @style = "display: initial;" } })
@Html.EditorFor(modelItem => attendingPeople.LastName, new { htmlAttributes = new { @class = "form-control col-md-5", @style = "display: initial;" } })
</div>
</div>
}
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save changes" class="btn btn-primary" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Iхотите сохранить обновленные данные в таблицу AttendingPeople в базе данных, но ничего не происходит.