0

有人可以帮助我如何使用 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”始终为假,因此不保存数据。请帮助我如何进行。

谢谢。

4

3 回答 3

2

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

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

它应该是相反的,只有当模型无效时,你应该停止该过程并使用表单重新渲染视图。

尝试:

if (!ModelState.IsValid)
    {
    return View(studentViewModel);
    }
于 2013-08-16T08:44:11.883 回答
0

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

于 2013-08-16T08:44:45.467 回答
0

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

尝试创建一个仅包含您希望在创建学生时由用户编辑的字段(扁平化版本)的视图模型。Required在您的视图模型中使用数据注释,例如StringLength验证用户输入。

于 2013-08-16T09:12:53.487 回答