2012-08-22 33 views
0

所以,我有两种方法:ViewModel分配了一个对象,我怎样才能将它传回到Post?

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

正如你可以看到,当我加载了我创建页面,我加载一个教室到视图模型。这将工作并在视图上显示正确的课堂。

当我POST到Create方法时,我的问题发生了,我的assignment对象现在不包含(它是NULL)我在GET中传入的课堂。

编辑〜要显示视图:

@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> 
} 
+0

您能否显示您的视图(您提交的内容)? –

+0

@KirillBestemyanov包含在编辑 –

回答

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) 
2

你只包括Classroom.Name,从而对表单提交无所不有该模型用粘结剂来填充你的模型。

你真的需要回教室吗?你想达到什么目的? 如果你需要的只是ID写出来的:

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

(或任何ID为),并加载备份在服务器端。

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

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

写出来作为输入,然后允许最终用户修改它,我不认为你想要的,所以我会使用的ID( GUID)。

0

如果希望课堂生存后,那么你需要将它添加到形成hiddenfield:

只需添加到这个形式:

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

几乎与上述相同的东西不是? –

+0

在上面的答案中,您需要通过ClassroomId从数据库获取控制器中的ClassroomName。你不需要它。但是选择什么样的答案取决于您想要如何处理“课堂”。 –

+0

当服务器端(即数据库)已存在的值存储名称时,容易被篡改,xss(脚本注入,悬挂html攻击等)。我通常只会保存身份证,并允许有希望的可信任课堂名称来自加入。 –

相关问题