代码之家  ›  专栏  ›  技术社区  ›  Jean-Francois

我想一次保存3个实体条目。如何做到这一点

  •  0
  • Jean-Francois  · 技术社区  · 14 年前

    我有一个3实体的视图。 当我单击提交按钮时,我想保存这些实体。

    在视图中

    <div class="editor-field ">
        <%: Html.TextBoxFor(model => model.ElementAt(0).UserQuestion1)%>
        <%: Html.ValidationMessageFor(model => model.ElementAt(0).UserQuestion1)%>
     </div>
    <div class="editor-field ">
        <%: Html.TextBoxFor(model => model.ElementAt(0).UserAnswer)%>
        <%: Html.ValidationMessageFor(model => model.ElementAt(0).UserAnswer)%>
    </div>
    
    <div class="editor-field ">
        <%: Html.TextBoxFor(model => model.ElementAt(1).UserQuestion1)%>
        <%: Html.ValidationMessageFor(model => model.ElementAt(1).UserQuestion1)%>
     </div>
     <div class="editor-field ">
        <%: Html.TextBoxFor(model => model.ElementAt(1).UserAnswer)%>
        <%: Html.ValidationMessageFor(model => model.ElementAt(1).UserAnswer)%>
      </div>
    
      <div class="editor-field ">
         <%: Html.TextBoxFor(model => model.ElementAt(2).UserQuestion1)%>
         <%: Html.ValidationMessageFor(model => model.ElementAt(2).UserQuestion1)%>
      </div>
      <div class="editor-field ">
         <%: Html.TextBoxFor(model => model.ElementAt(2).UserAnswer)%>
         <%: Html.ValidationMessageFor(model => model.ElementAt(2).UserAnswer)%>
      </div>
    

    在控制器中

        public ActionResult ChooseQuestion()
        {
            List<UserQuestion> lst = new List<UserQuestion>() {
                new UserQuestion(),new UserQuestion(), new UserQuestion()
            };
            return View(lst);
        }
    
        [HttpPost]
        public void ChooseQuestion(List<UserQuestion> lst)
        {
            //lst is always NULL Why
            //EntityFactory.GetEntity().SaveChanges();
        }
    

    为什么当我单击提交按钮时,我的参数列表lst为空。 我想做一次扑救。

    谢谢。

    2 回复  |  直到 14 年前
        1
  •  0
  •   Brian Mains    14 年前

    请尝试使用模型[0]语法,而不是处的元素。这在我看到的示例中使用。

    Hth.

        2
  •  0
  •   Graham Conzett    14 年前

    如果有已知数量的实体,则可以为每个实体构建具有不同名称的自定义视图模型。然后提交它们时,它们应该被正确绑定。