我使用this pattern模型绑定到列表,但是当我发布表单时,IList始终为null。
我之前成功使用过这种模式。这次它与我过去做的有点不同,因为这些字段是在子循环中生成的。不确定这是不是为什么它不起作用。我做了很多试验和试验错误,所以我希望有人能看到我做错了什么:
@model Collect.ViewModels.JobWorker
@{
ViewBag.Title = $"{Model.Job.Name} - Workers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("Update", "Workers"))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => Model.JobId)
int z = 0;
for (int x = 0; x < Model.WorkPeriods.Count; x++)
{
<h3>@Model.WorkPeriods[x].StartDate.toIsoDateString() through @Model.WorkPeriods[x].EndDate.toIsoDateString()</h3>
<div style="display: flex; flex-wrap: wrap;">
@for (int y = 1; y <= Model.Job.MaximumNumber; y++)
{
<div class="panel panel-primary panel-info-1" >
<div class="panel-heading">
<span class="fa fa-clock-o"></span> Opening @Model.Workers[z].OpeningNumber
</div>
<div class="panel-body">
@Html.DropDownListFor(model => Model.Workers[z].PersonId,
new SelectList(Model.People, "PersonId", "Name"),
htmlAttributes: new { @class = "form-control" })
</div>
</div>
@Html.HiddenFor(model => Model.Workers[z].Id);
@Html.HiddenFor(model => Model.Workers[z].JobId);
@Html.HiddenFor(model => Model.Workers[z].WorkerId);
@Html.HiddenFor(model => Model.Workers[z].OpeningNumber);
@Html.HiddenFor(model => Model.Workers[z].StartDate);
@Html.HiddenFor(model => Model.Workers[z].EndDate);
z++;
}
<div class="panel-info-1" >
<button type="submit" value="Update" class="btn btn-default">Update</button>
</div>
</div>
}
}
<div>
@Html.ActionLink("Back to Jobs", "Index", "Jobs")
</div>
public class JobWorker
{
public byte JobId { get; set; }
public Job Job { get; set; }
public List<Person2> People { get; set; }
public List<WorkPeriod> WorkPeriods { get; set; }
public IList<Dto.Worker> Workers { get; set; }
}
public class Worker
{
public Worker(byte id)
{
Id = id;
}
[Key]
public byte Id { get; set; }
public int? WorkerId { get; set; }
public int? PersonId { get; set; }
public byte JobId { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public byte OpeningNumber { get; set; }
}
public ActionResult Index(int? Id)
{
var jobEnt = db.Jobs.Where(x => x.JobId == Id).FirstOrDefault();
var jobWorker = new JobWorker()
{
JobId = jobEnt.JobId,
Job = Mapper.Map<Ent.Job, Mod.Job>(jobEnt),
WorkPeriods = new List<WorkPeriod> { startPeriod },
Workers = new List<Dto.Worker>(),
People = db.People.ToList()
};
// business logic to populate WorkPeriods and Workers here.
return View(jobWorker);
}
<form action="/Workers/Update" method="post">
<input data-val="true" data-val-number="The field JobId must be a number." data-val-required="The JobId field is required."
id="JobId" name="JobId" type="hidden" value="7" />
<h3>2018-05-01 through 2018-05-31</h3>
<div style="display: flex; flex-wrap: wrap;">
<div class="panel panel-primary">
<div class="panel-heading">
<span class="fa fa-clock-o"></span> Opening 1
</div>
<div class="panel-body">
<select class="form-control" data-val="true" data-val-number="The field PersonId must be a number." id="Workers_0__PersonId"
name="Workers[0].PersonId">
<option value=""></option>
<option value="1">Person 1</option>
<option value="2">Person 2</option>
</select>
</div>
</div>
<input data-val="true" data-val-number="The field JobId must be a number." data-val-required="The JobId field is required."
id="Workers_0__JobId" name="Workers[0].JobId" type="hidden" value="7" />
<input data-val="true" data-val-number="The field WorkerId must be a number." id="Workers_0__WorkerId" name="Workers[0].WorkerId"
type="hidden" value="" />
<input data-val="true" data-val-number="The field OpeningNumber must be a number." data-val-required="The OpeningNumber field is required."
id="Workers_0__OpeningNumber" name="Workers[0].OpeningNumber" type="hidden" value="1" />
<input id="Workers_0__StartDate" name="Workers[0].StartDate" type="hidden" value="2018-05-01" />
<input id="Workers_0__EndDate" name="Workers[0].EndDate" type="hidden" value="2018-05-31" />
<div class="panel panel-primary" style="width: 400px; margin: 20px;">
<div class="panel-heading">
<span class="fa fa-clock-o"></span> Opening 2
</div>
<div class="panel-body">
<select class="form-control" data-val="true" data-val-number="The field PersonId must be a number." id="Workers_1__PersonId"
name="Workers[1].PersonId">
<option value=""></option>
<option value="1">Person 1</option>
<option value="2">Person 2</option>
</select>
</div>
</div>
<input data-val="true" data-val-number="The field JobId must be a number." data-val-required="The JobId field is required."
id="Workers_1__JobId" name="Workers[1].JobId" type="hidden" value="7" />
<input data-val="true" data-val-number="The field WorkerId must be a number." id="Workers_1__WorkerId" name="Workers[1].WorkerId"
type="hidden" value="" />
<input data-val="true" data-val-number="The field OpeningNumber must be a number." data-val-required="The OpeningNumber field is required."
id="Workers_1__OpeningNumber" name="Workers[1].OpeningNumber" type="hidden" value="2" />
<input id="Workers_1__StartDate" name="Workers[1].StartDate" type="hidden" value="2018-05-01" />
<input id="Workers_1__EndDate" name="Workers[1].EndDate" type="hidden" value="2018-05-31" />
<div style="width: 400px; margin: 20px;">
<button type="submit" value="Update" class="btn btn-default">Update</button>
</div>
</div>
</form>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(byte JobId, IList<Dto.Worker> newWorkers)
{
// newWorkers is always null
// logic to update is here
return RedirectToAction("Index", "Jobs");
}
答案 0 :(得分:2)
基于视图中使用的模型,
input,
#dqdiv_volgraph {
display: none;
}
label {
border: 1px solid black;
padding: 5px;
cursor: pointer;
}
input:checked+#dqdiv_volgraph {
display: block;
}
<div id="table_and_dqinfo" class="table_and_dqinfo">
<div id="dqdiv" class="dqinfo">
<h4 class="centertext">Information Pane</h4>
<input id="checkb" type="checkbox">
<div id="dqdiv_volgraph">
<h1>Put image here</h1>
<img src="https://placehold.it/200x200">
</div>
<label for="checkb"> Click me</label>
</div>
</div>
操作期望错误的模型,这就是模型绑定器没有转换它的原因。
@model Collect.ViewModels.JobWorker