【问题标题】:MVC: Update Grand Children Using UpdateModelMVC:使用 UpdateModel 更新 Grand Children
【发布时间】:2016-07-15 15:15:39
【问题描述】:

我有一个嵌套视图模型,我正在使用它来更新(编辑)表单。我可以使用 UpdateModel 更新父母和孩子,但无法更新 Grand Children。我为 UpdateModel 前缀名称尝试了“Answers”和“Questions.Answers”,但它没有更新。任何帮助表示赞赏!

   [HttpPost]
    public ActionResult Edit(SurveyViewModel request)
    {
        try
        {
            var dbrecord = Survey.Get(request.SurveyID, Survey.FetchType.SurveyID);
            UpdateModel(dbrecord); // Working
            UpdateModel(dbrecord.SurveyQuestions, "Questions"); // Working

            // Not Working
            for (var i = 0; i < dbrecord.SurveyQuestions.Count; i++)
            {
                UpdateModel(dbrecord.SurveyQuestions[i].SurveyAnswers, "Questions[" + i + "].Answers");
            }

            dbrecord.Save();

            return View();
        }
        catch (Exception ex)
        {
        }
    }

查看模型

public class SurveyViewModel
{
    public int SurveyID { get; set; }

    public string Name { get; set; }

    public List<QuestionsRequest> Questions { get; set; }

    public class QuestionsRequest
    {
        public string Question { get; set; }

        public List<AnswersRequest> Answers { get; set; }

        public class AnswersRequest
        {
            public string Answer { get; set; }
        }
}

【问题讨论】:

    标签: c# asp.net-mvc updatemodel


    【解决方案1】:

    这更像是一种解决方法,而不是最佳实践,但您是否尝试在调用 UpdateModel 之前为 for 循环中的每个项目将实体状态设置为 EntityState.Modified?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多