保存ViewModels的ASP.NET MVC 4错误

时间:2013-08-16 08:34:23

标签: asp.net-mvc entity-framework asp.net-mvc-4

有人可以帮我了解如何使用ViewModel将数据保存和更新到多个实体吗?

我有一个看起来像这样的ViewModel:

  public class StudentViewModel
  {
    public Student student;
    public StudentAddress studentAddress { get; set; }
    public StudentPhoto studentPhoto { get; set; }
    // Three entities are related to one to one relationship

    public StudentViewModel()
    { }

    }

我的控制器是:

  [HttpPost]
    public ActionResult Create(StudentViewModel studentViewModel)
    {
        if (ModelState.IsValid)
        {
            return View(studentViewModel);
        }

            Student s = new Student()
             {
                 Name =studentViewModel.Student.Name,
                 Speciality = studentViewModel.Student.Speciality,
                 DateOfReg = studentViewModel.Student.DateOfJoinig,
                 Qualification = studentViewModel.Student.Qualification,
                 Email = studentViewModel.Student.Email

             };

            StudentAddress sa = new StudentAddress()
            {
                StudentId= studentViewModel.Student.StudentId,
                Address = studentViewModel.StudentAddress.Address,
                Area = studentViewModell.StudentAddress.Area,
                City = studentViewModel.StudentAddress.City,
                State = studentViewModel.StudentAddress.State,
                Mobile = studentViewModel.StudentAddress.Mobile

            };

            StudentPhoto sp = new StudentPhoto()
            {
                StudentId= studentViewModel.Student.StudentId,
                Photo = studentViewModel.StudentPhoto.Photo

            };    
            db.Students.Add(s);
            db.StudentAddress.Add(sa);
            db.StudentPhoto.Add(sp);

            db.SaveChanges();
            return RedirectToAction("Home");
    }

查看是:

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Doctor</legend>


    @Html.EditorFor(model => model.Student.Name )
    @Html.EditorFor(model => model.Student.Speciality)
    @Html.EditorFor(model => model.Student.DateOfJoinig)
    @Html.EditorFor(model => model.Student.Standard)

    @Html.HiddenFor(model => model.Student.StudentId)
    @Html.EditorFor(model => model.StudentAddress.Address)
    @Html.EditorFor(model => model.StudentAddress.Area)
    @Html.EditorFor(model => model.StudentAddress.City)
    @Html.EditorFor(model => model.StudentAddress.State)

    @Html.HiddenFor(model => model.Student.StudentId)
    @Html.EditorFor(model => model.StudentPhoto.Photo)

     <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

我能够检索并将数据(来自多个实体)显示到视图中。但是,现在我仍然坚持如何使用新数据保存和更新上述实体。大多数示例都是1-1关系,映射是自动的,但在这种情况下,数据属于多个实体。

我的问题是当我尝试保存数据时,它会重定向到创建页面。 “ModelState.IsValid”始终为false,因此不保存任何数据。请帮我,我该怎么办。

感谢。

3 个答案:

答案 0 :(得分:2)

行动顶部的这一行是错误的:

if (ModelState.IsValid)
    {
    return View(studentViewModel);
    }

它应该相反,只有当模型 NOT 有效时,才应该停止该过程并使用表单重新呈现视图。

尝试:

if (!ModelState.IsValid)
    {
    return View(studentViewModel);
    }

答案 1 :(得分:0)

在Controller中,检查if(modelstate.isvalid) - 如果有效则返回视图而不保存视图中的数据。

答案 2 :(得分:0)

您的实现存在的问题是您的视图模型包含多个模型(实体)。这不是一个好的实现。

尝试创建一个viewmodel,它只包含用户在创建学生时要编辑的字段(展平版本)。在视图模型中使用数据注释,例如RequiredStringLength来验证用户输入。