【问题标题】:I want to save 3 entities entry at a time. How to do that我想一次保存 3 个实体条目。怎么做
【发布时间】:2010-11-02 10:14:23
【问题描述】:

我有 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();
    }

为什么当我点击提交按钮时,我的参数 List lst 为空。 我想执行保存。

谢谢。

【问题讨论】:

    标签: asp.net asp.net-mvc-2 linq-to-entities entity-framework-4


    【解决方案1】:

    尝试使用 model[0] 语法而不是 element at。这在我看到的示例中使用。

    HTH。

    【讨论】:

    • 这正是我所需要的。谢谢。
    【解决方案2】:

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

    【讨论】:

    • 我不知道实体的数量,我在这个例子中使用了 3。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多