在一个视图中编辑两个模型

时间:2018-04-03 14:46:15

标签: c# asp.net-mvc entity-framework

我在EF6中使用ASP.NET MVC,我想在一个视图中使用2个模型,并允许用户编辑它们,因此我创建了一个结合了2个模型的模型。

提交编辑表单时,第一个模型编辑正确,但第二个模型抛出此错误

  

System.NullReferenceException:'对象引用未设置为   对象的实例。' studentsAndID(studentsAndID是第二名   model)无效。

型号

namespace myproject.Models
{
    public class ProjectAndStudentModels
    {
        // first model is Project table from dtaabase
        public Project project { get; set; }
        // second model is Dictionary because I want to pass Dictionary values to view
        public Dictionary<string, int> StudentsAndID { get; set; }
    }
}

然后我从控制器传递模型值以正确查看。

控制器

// this action pass all information in dictionary and project correctly 
public ActionResult Edit(int? id)
{   // get first model
    Project proj = db.SeniorProject.Find(id);
    // get second model
    Dictionary<string, int> students = GetStu(id);
    // set the above models inside one model            
    var TwoModel = new ProjectAndStudentModels { project = proj, StudentsAndID = students };
    return View(TwoModel);
}

public Dictionary<string, int> GetStu(int? id)
{
    /* some code that return Dictionary */
    return obj.StudentList;
}

[HttpPost]
public ActionResult Edit(ProjectAndStudentModels both)
{
    // editing on project is correct
    projEdit(both.project);
    // but editing on dictionary not
    StuEdit(both.StudentsAndID);
    return RedirectToAction("success");
}

private void StuEdit(Dictionary<string, int> studentsAndID)
{

    if (ModelState.IsValid)
    {
        Student stu = new Student();
        // this throw System.NullReferenceException: 'Object reference not set to an instance of an object.' studentsAndID was null.
        for (var i = 0; i < studentsAndID.Count; i++)
        {
            var item = studentsAndID.ElementAt(i);

            stu.studentId = item.Value;
            stu.studentName = item.Key;
            using (SaVeITDBEntities entity = new SaVeITDBEntities())
            {
                entity.Student.Add(stu);
                entity.SaveChanges();
            }
        }
    }

}
//this works correctly
public void projEdit([Bind(Include = "Id,Name,Year,Abstract")] Project proj)
{
    if (ModelState.IsValid)
    {
        db.Entry(proj).State = EntityState.Modified;
        db.SaveChanges();
    }
}

查看

@model myproject.Models.ProjectAndStudentModels
@{
    ViewBag.Title = "Edit";
}

@using (Html.BeginForm())
{
  @Html.HiddenFor(model => model.project.Id)

// the dictionary not empty but when submiting the form it sent to controller as null I do not know why? 
    @foreach (var tuple in Model.StudentsAndID)
    {
  @Html.LabelFor(model => @tuple.Value, new {})  @Html.EditorFor(model => @tuple.Value)

  @Html.LabelFor(model => @tuple.Key, new {}) @Html.EditorFor(model => @tuple.Key)
                }

  @Html.LabelFor(model => model.project.Name, new { }) @Html.EditorFor(model => model.project.Name)
   <!--other code for project data-->
   <input type="submit" value="Save"/>    }

这不是重复的问题我的问题关于asp.net mvc而不是VB.Net

0 个答案:

没有答案