所以我有两种方法:
public ActionResult Create(Guid id)
{
Assignment viewModel = new Assignment();
viewModel.Classroom = classroomRepository.GetByID(id);
return View(viewModel);
}
[HttpPost]
public ActionResult Create(Assignment assignment)
{
if (ModelState.IsValid)
{
assignmentRepository.Insert(assignment);
assignmentRepository.Save();
return RedirectToAction("Index");
}
return View(assignment);
}
如您所见,当我加载 Create 页面时,我正在将 Classroom 加载到 viewModel 中。这有效并在视图上显示正确的教室。
当我 POST 到 Create 方法时出现我的问题,我assignment
现在传入的对象不包括(它是 NULL)我在 GET 上传入的 Classroom。
编辑〜显示视图:
@model My_School_Book.Models.Assignment
@{
ViewBag.Title = "Create";
}
<div class="span9">
<div class="row-fluid">
<h1>Creating New Assignment:</h1>
<hr />
@using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.ValidationSummary(true)
<div class="control-group">
@Html.LabelFor(model => model.Classroom.Name, "Classroom", new { @class = "control-label" })
<div class="controls">
<span style="font-size: 26px;">@Html.DisplayTextFor(model => model.Classroom.Name)</span>
</div>
</div>
<div class="control-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="control-group">
@Html.LabelFor(model => model.Description, new { @class = "control-label" })
<div class="controls">
@Html.TextAreaFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
</div>
<div class="control-group">
@Html.LabelFor(model => model.AssignedDate, new { @class = "control-label" })
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-calendar"></i></span>@Html.TextBoxFor(model => model.AssignedDate)
</div>
@Html.ValidationMessageFor(model => model.AssignedDate)
</div>
</div>
<div class="control-group">
@Html.LabelFor(model => model.DueDate, new { @class = "control-label" })
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-calendar"></i></span>@Html.TextBoxFor(model => model.DueDate)
</div>
@Html.ValidationMessageFor(model => model.DueDate)
</div>
</div>
<div class="control-group">
@Html.LabelFor(model => model.Weight, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.Weight, new { @class = "span2" })
@Html.ValidationMessageFor(model => model.Weight)
</div>
</div>
<div class="control-group">
@Html.LabelFor(model => model.Total, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.Total, new { @class = "span2" })
@Html.ValidationMessageFor(model => model.Total)
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary"><i class="icon-plus-6"></i> Create</button>
<a href="@Url.Action("Index", "Assignment")" class="btn btn-primary"><i class="icon-back"></i> Cancel</a>
</div>
}
</div>
</div>
@section Scripts
{
<script type="text/javascript">
$(function () {
$("#AssignedDate").datepicker({
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: "DD MM d, yy",
minDate: -14,
onSelect: function (selectedDate) {
$("#DueDate").datepicker("option", "minDate", selectedDate);
}
});
$("#DueDate").datepicker({
dateFormat: "DD MM d, yy",
minDate: 0,
onSelect: function (selectedDate) {
$("#AssignedDate").datepicker("option", "maxDate", selectedDate);
}
});
});
</script>
}