ViewModel分配了一个对象,我该如何将其传回Post?

时间:2012-08-22 13:47:35

标签: asp.net asp.net-mvc-3

所以我有两种方法:

    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);
    }

正如您所看到的,当我加载我的创建页面时,我正在将一个Classroom加载到viewModel中。这可以在视图上显示正确的课堂。

当我向Create方法发布POST时出现问题,我传入的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>
}

3 个答案:

答案 0 :(得分:2)

您只包含Classroom.Name,因此在表单上没有任何内容可供模型活页夹填充您的模型。

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

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

(或无论id是什么)并将其加载回服务器端。

如果你真的需要它 - 你可以通过

将其作为输入写出来
@Html.EditorFor(o=>o.Classroom)

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

答案 1 :(得分:0)

如果你希望Classroom在post中存活,那么你需要将它添加到hiddenfield中:

只需将其添加到表单中:

@Html.HiddenFor(m=m.Classroom.ClassroomId)
@Html.HiddenFor(m=m.Classroom.ClassroomName)

答案 2 :(得分:0)

我最终使用了View Model:

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)