0

所以我有两种方法:

    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>&nbsp;Create</button>
                <a href="@Url.Action("Index", "Assignment")" class="btn btn-primary"><i class="icon-back"></i>&nbsp;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>
}
4

3 回答 3

2

您只包括 Classroom.Name,因此在表单上没有任何内容可供模型绑定器用来填充您的模型。

你真的需要重新安置教室吗?你想达到什么目的?如果您需要 ID,只需将其写为:

@Html.HiddenFor(o=>o.Classroom.ClassroomId)

(或任何 id )并将其加载到服务器端。

如果您真的需要它 - 您可以通过以下方式将其写为输入

@Html.EditorFor(o=>o.Classroom)

然后允许最终用户修改我认为你不想要的它,因此我会使用 ID(guid)。

于 2012-08-22T14:06:21.423 回答
0

如果您希望课堂在帖子中存活,则需要将其添加到隐藏字段中的表单:

只需将其添加到表单中:

@Html.HiddenFor(m=m.Classroom.ClassroomId)
@Html.HiddenFor(m=m.Classroom.ClassroomName)
于 2012-08-22T14:25:10.507 回答
0

我最终使用了一个视图模型:

public class AssignmentCreateData
{
    public Guid ClassroomID { get; set; }
    public String ClassroomName { get; set; }
    public Assignment Assignment { get; set; }
}

我的控制器看起来像这样:

    public ActionResult Create(Guid id)
    {
        AssignmentCreateData viewModel = new AssignmentCreateData();
        Classroom classroom = classroomRepository.GetByID(id);
        viewModel.ClassroomID = classroom.ClassroomID;
        viewModel.ClassroomName = classroom.Name;
        viewModel.Assignment = null;
        return View(viewModel);
    } 

    [HttpPost]
    public ActionResult Create(AssignmentCreateData viewModel)
    {
        if (ModelState.IsValid)
        {
            viewModel.Assignment.ClassroomID = viewModel.ClassroomID;
            assignmentRepository.Insert(viewModel.Assignment);
            assignmentRepository.Save();
            return RedirectToAction("Index");
        }
        return View(viewModel);
    }

然后我的视图包括:

@Html.HiddenFor(model => model.ClassroomID) 
于 2012-08-22T16:19:46.047 回答