Asp.net mvc ASP.NET MVC 4保存视图模型时出错

Asp.net mvc ASP.NET MVC 4保存视图模型时出错,asp.net-mvc,entity-framework,asp.net-mvc-4,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 re

有人能帮助我如何使用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>
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
医生
@EditorFor(model=>model.Student.Name)
@EditorFor(model=>model.Student.Speciality)
@EditorFor(model=>model.Student.DateOfJoinig)
@EditorFor(model=>model.Student.Standard)
@Html.HiddenFor(model=>model.Student.StudentId)
@EditorFor(model=>model.StudentAddress.Address)
@EditorFor(model=>model.StudentAddress.Area)
@EditorFor(model=>model.StudentAddress.City)
@EditorFor(model=>model.StudentAddress.State)
@Html.HiddenFor(model=>model.Student.StudentId)
@EditorFor(model=>model.StudentPhoto.Photo)

} @ActionLink(“返回列表”、“索引”)
我能够在视图中检索和显示数据(来自多个实体)。但是,现在我一直在思考如何使用新数据保存和更新上述实体。大多数示例都是1-1关系—映射是自动的,但在本例中,数据属于多个实体

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


谢谢。

您操作顶部的这一行是错误的:

if (ModelState.IsValid)
    {
    return View(studentViewModel);
    }
相反,只有当模型无效时,才应停止该过程并使用表单重新渲染视图

尝试:


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

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


尝试创建仅包含用户在创建学生时要编辑的字段(展开版本)的viewmodel。在视图模型中使用数据注释,如
Required
StringLength
,以验证用户输入。

不完全正确,如果数据需要用于多个实体,请尝试使用模型的展开版本。通常是。虽然
Student
实体的结构可能会更好,但最好使用
Student.Address
Student.Photo
引用,而不是依赖外键。
if (!ModelState.IsValid)
    {
    return View(studentViewModel);
    }