Asp.net mvc ASP.NET MVC创建和编辑一对多关系无效模型状态

Asp.net mvc ASP.NET MVC创建和编辑一对多关系无效模型状态,asp.net-mvc,entity-framework,ef-code-first,one-to-many,modelstate,Asp.net Mvc,Entity Framework,Ef Code First,One To Many,Modelstate,为什么ModelState.IsValid在使用post存储一对多数据时返回“false”。我使用ViewModel和EditorTemplate来显示这些数据,以便进行编辑。在我的案例测试(一)-问题(多)。如果只存储了测试,modelstate是有效的,但如果我将问题存储在测试中,则模型状态将变为无效。。。到处寻找信息,但不明白为什么这是无效的。既然许多教程显示这是映射一对多关系的方法,为什么我不能以这种方式发布数据 模型 public class Test { public Test

为什么ModelState.IsValid在使用post存储一对多数据时返回“false”。我使用ViewModel和EditorTemplate来显示这些数据,以便进行编辑。在我的案例测试(一)-问题(多)。如果只存储了测试,modelstate是有效的,但如果我将问题存储在测试中,则模型状态将变为无效。。。到处寻找信息,但不明白为什么这是无效的。既然许多教程显示这是映射一对多关系的方法,为什么我不能以这种方式发布数据

模型

public class Test
{
   public Test()
   {
      this.Questions = new List<Question>();
   }
//some excluded for brevity

    [Required]
    public string Name { get; set; }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    public DateTime TestDate { get; set; }

    public int TestTypeId { get; set; }

    public virtual ICollection<Question> Questions { get; set; }
    public virtual Type Type { get; set; }
   }
}

public class Question
{
   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int QuestionID { get; set; }

    public int TestID { get; set; }

    public string Question { get; set; }

    public virtual Test Test { get; set; }
}

您看到ModelState中的错误是什么了吗?使用以下内容了解错误是什么var errors=ModelState.Values.Where(x=>x.errors.Any());我使用了您提供的代码,并在调试中查看了它。。。错误计数=1,错误消息=“”,异常=System.InvalidOperationException:从类型“System.String”到类型“Project.Models.Test”的参数转换失败,因为没有类型转换器可以在这些类型之间转换。同样,如果我不包括问题的值,我将一无所获。。。嗯,谢谢你帮我找出这个问题的原因!但在我看来,问题来自model.test.question,因为问题需要fk测试id。。。如果我只是使用model.questions(带有问题类型),它不会为“编辑”视图返回任何内容。我很想评论你的答案,但现在已经不在了。
  public class TestData
{
    public Test Test { get; set; }
    public Question Question { get; set; }
    public IEnumerable<TestType> TestTypes { get; set; }
    public List<string> Questions { get; set; }
}
@model Project.Models.CustomQuestion
<div>
 @Html.HiddenFor(a => a.QuestionID)
 @Html.HiddenFor(a => a.TestID)
 @Html.HiddenFor(a => a.Test)
 @Html.EditorFor(a => a.Question)
</div>
@model  Project.ViewModels.TestData

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

<fieldset>
    <legend>Event</legend>

    @Html.HiddenFor(a => a.Test.Id)

    <div class="editor-field">
        @Html.EditorFor(model => model.Test.Name
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.Test.TestDate)
    </div>

    <div>
        @Html.DropDownListFor(model => model.Test.TestTypeId, new SelectList(Model.TestTypes, "Id", "Name"))
    </div>

    <div class="editor-field">
        if 5 questions are stored 5 textboxes with questions will appear
        @Html.EditorFor(model => model.Test.Questions)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}
    public ActionResult Edit(int id = 0)
    {

       var viewModel = new ViewModels.TestData
       {
          Test = db.Tests.Find(id),
          TestTypes = db.TestTypes
       };
           return View(viewModel);
    }

    [HttpPost]
    public ActionResult Edit(TestData testData)
    {

       if (ModelState.IsValid) //returns "false" if testData.Event.Questions > 0
       {
          db.Entry(testtData).State = EntityState.Modified;
          db.SaveChanges();
          return RedirectToAction("Index");
       }

       return View(testData);
     }