出于某种原因,我的部分视图总是将空值返回给控制器。我已经尝试以几种不同的方式(包括编辑器模板)来实现它,但这是我最新的。
基本上我正在使用 jquery ui datepicker 渲染视图。当用户选择一个日期时,我会渲染我的部分视图。部分视图正在正确呈现,但是当我提交部分视图时,它向控制器返回了一个空对象
查看型号:
public class Attendance
{
public int RecordId { get; set; }
public int GroupId { get; set; }
public int MemberId { get; set; }
public string MemberName { get; set; }
public System.DateTime Date { get; set; }
public bool Present { get; set; }
}
父视图:
@{
ViewBag.Title = "Take Attendance";
}
<h2>Take Attendance</h2>
@Html.Hidden("GroupId", (int)ViewBag.GroupId)
<div>
@Html.Label("Select A Date")
@Html.JQueryUI().Datepicker("attendanceDate",ViewBag.AttendanceDate)
.OnSelect("attendanceDate.onSelect")
</div>
<div id="attendanceRecords">
</div>
@section scripts
{
<script src="@Url.Content("../../Scripts/Views/TakeAttendance.js")" type="text/javascript"></script>
}
局部视图:
@model IEnumerable<AT.Manager.Models.Attendance>
@using (Html.BeginForm("TakeAttendance", "Groups")) {
@Html.ValidationSummary(true)
<table>
<tr>
<th>Name</th>
<th>Present?</th>
</tr>
@for (int i = 0; i < Model.Count(); i++ )
{
<tr>
<td>@Html.DisplayFor(model => Model.ElementAt(i).MemberName)</td>
<td>@Html.EditorFor(model => Model.ElementAt(i).Present)</td>
<td>@Html.HiddenFor(model => Model.ElementAt(i).GroupId)</td>
<td>@Html.HiddenFor(model => Model.ElementAt(i).MemberId)</td>
<td>@Html.HiddenFor(model => Model.ElementAt(i).RecordId)</td>
</tr>
}
</table>
<p>
<input type="submit" value="Create" />
</p>
}
控制器动作:
public ActionResult TakeAttendance(IEnumerable<Attendance> records)
{
if (ModelState.IsValid)
{
var newRecords = records.Select(x => new da.Attendance() { Id = x.RecordId, GroupId = x.GroupId, MemberId = x.MemberId, Date = x.Date });
AttendanceRepository attendanceRepo = new AttendanceRepository();
newRecords.Each(x => attendanceRepo.InsertOrUpdate(x));
return RedirectToAction("Index");
}
else
{
return View();
}
}
任何建议将不胜感激。