Я создал Модель, Вид и Контроллер. На мой взгляд, я перебираю коллекцию и создаю таблицу с каждым из перечисленных вместе с кнопкой Сохранить.
Когда я нажимаю кнопку Сохранить, единственные данные, возвращаемые контроллеру, это ID и LandownerID - все остальные поля отображаются как нулевые.
Я провел большую часть сегодняшнего дня в поисках Google и пробовал несколько ответов, ни один из которых не помог.
Контроллер:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,ExemptionNumber,IssueDate,KillCount,TRAPPING,SHOOTING,DOGS,OTHER,NO_INDICATION,NOTES,SPECIES,E_LANDOWNER,EXEM_YEAR,MethodOfDisposal,NO_DATA")] ExempKillData exempKillData)
{
if (ModelState.IsValid)
{
db.Entry(exempKillData).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index","Owners");
}
return View(exempKillData);
}
Вид:
@model IEnumerable<Exemptions.Models.ExempKillData>
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table border="1" id="tbl">
<tbody>
..table headers snipped...
@foreach (var kill in Model)
{
<tr>
<td>
@Html.EditorFor(model => Model.First().ID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
</td>
<td>
@Html.EditorFor(model => Model.First().ExemptionNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
</td>
<td valign="top">
@Html.EditorFor(model => kill.IssueDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.IssueDate, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.KillCount, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.KillCount, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.TRAPPING, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.TRAPPING, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.SHOOTING, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.SHOOTING, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.DOGS, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.DOGS, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.OTHER, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.OTHER, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.NO_INDICATION, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.NO_INDICATION, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.NOTES, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.NOTES, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.SPECIES, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.SPECIES, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.E_LANDOWNER, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.E_LANDOWNER, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.EXEM_YEAR, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.EXEM_YEAR, "", new { @class = "text-danger" })
</td>
<td valign="top">
@Html.EditorFor(model => kill.NO_DATA, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => kill.NO_DATA, "", new { @class = "text-danger" })
</td>
<td valign="top" align="center">
<input type="submit" value="Save" class="btn btn-default" />
</td>
</tr>
}
</tbody>
</table>
}
Модель:
[Table("WHE.ExempKillData")]
public partial class ExempKillData
{
[Display(Name ="Exemption Number")]
public int? ExemptionNumber { get; set; }
[Display(Name = "Issue Date")]
[Column(TypeName = "datetime2")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? IssueDate { get; set; }
[Display(Name = "Kill Count")]
public int? KillCount { get; set; }
public int? TRAPPING { get; set; }
[Column(TypeName = "numeric")]
public int? SHOOTING { get; set; }
public int? DOGS { get; set; }
[Column(TypeName = "numeric")]
public int? OTHER { get; set; }
[Display(Name ="No Indication")]
public int? NO_INDICATION { get; set; }
[StringLength(200)]
public string NOTES { get; set; }
[StringLength(32)]
public string SPECIES { get; set; }
[Display(Name ="Landowner")]
public double? E_LANDOWNER { get; set; }
[Display(Name ="Exemption Year")]
[StringLength(4)]
public string EXEM_YEAR { get; set; }
[Display(Name ="No Data")]
[StringLength(40)]
public string NO_DATA { get; set; }
public string MethodOfDisposal { get; set; }
public int ID { get; set; }
}
}
Я ожидаю, что нажатие кнопки «Сохранить» вернет содержимое этой строки обратно в действие контроллера, где затем можно будет правильно сохранить данные.
ModelState.IsValid имеет значение true для каждой записи, которую я пытаюсь редактировать, но, как упоминалось выше, все поля, кроме ID и E_Landowner, имеют значение null.