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

实体框架4:保存一个到多个实体不起作用

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

    下面是一个常见的场景
    我有这些实体

    用户ID
    用户名


    用户问题
    用户Qid
    用户ID
    用户问题


    一个用户可以有许多问题,但一个问题附加到一个用户。
    在我的示例中,我创建了3个空实体(UserQuestion)。然后我把这些问题附加到用户身上。
    在视图中,对于每个问题、答案文本框,我使用ElementAt指定每个UserQuestion实体。
    为什么我不能保留这三个问题


    在控制器中

    public ActionResult ChooseQuestion()
    {
        IUserRepository repUser = new UserRepository(EntityFactory.GetEntity());
        User usr = repUser.GetUserByID(Convert.ToInt32(Session["UserID"]));
        usr.UserQuestions.Add(new UserQuestion());
        usr.UserQuestions.Add(new UserQuestion());
        usr.UserQuestions.Add(new UserQuestion());
        var ViewModel = new UsrViewModel()
                        {
                            UserInfo = usr
                        };   
        return View(ViewModel);
    
    }
    
    [HttpPost]
    public void ChooseQuestion(User UserInfo)
    {
        UpdateModel(User, "UserInfo");
        EntityFactory.GetEntity().SaveChanges();
    }
    

    在我看来

    <%: Html.TextBoxFor(model => model.UserInfo.UserName)%>
    ...
    <%: Html.TextBoxFor(model => model.UserInfo.LastName%>
    ...
    <%: Html.TextBoxFor(model => model.UserInfo.UserPassword%>
    ..
    <h2>Question Creation</h2>
    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(0).UserQuestion)%>
    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(0).UserAnswer)%>
    
    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(1).UserQuestion)%>
    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(1).UserAnswer)%>
    
    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(2).UserQuestion)%>
    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(2).UserAnswer)%>
    

    5 回复  |  直到 14 年前
        1
  •  3
  •   Boris Callens    14 年前

    基于 Phil Haack's post

    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(0).UserQuestion, new {name="UserInfo.UserQuestion0.UserQuestion} )%>
    

    通常,如果内置的HtmlHelper.TextboxFor方法在for循环中,它将正确地执行name属性。

    因为我现在离我的VS不近,我不能自己尝试这个魔术,但是如果你被卡住了,你可能想看看菲尔的文章。

        2
  •  0
  •   Randy    14 年前

    不确定你到底在找什么:

    你好像少了一个问题表:

    question
    ---------
    question_id
    question_text
    

    然后将其链接到用户

    user_question
    --------------
    user_id
    question_id
    answer
    
        3
  •  0
  •   The Smallest    14 年前

    错误在于

    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(0).UserQuestion)%>
    <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions.ElementAt(1).UserQuestion)%>
    

    生成HTML代码:

    <input id="UserQuestion" name="UserQuestion" type="text" value="UserQuestion" />
    <input id="UserQuestion" name="UserQuestion" type="text" value="UserQuestion" />
    

    id s、 这是禁止的。因此,当您将数据发送回控制器时,只有一个值通过PostParameters发送:UserQuestion=,没有索引,没有前缀,没有 UserQuestions.UserQuestion[0]
    为了深入研究类似的问题,我建议使用 Fiddler

    代码的第二个问题是 UpdateModel(User, "UserInfo") 和UserInfo参数(属于User类型)。(顺便问一下:这段代码是如何编译的?当然不应该)。 FormCollection 调用时作为参数 UpdateModel . 因为在你的情况下变量 UserInfo 已更新。
    至于我,你在做一些奇怪的事情,试图编辑整个收藏。

        4
  •  0
  •   Stefanvds    14 年前

    按如下方式替换代码:

    <h2>Question Creation</h2>
    <% for (int i = 0; i < Model.UserInfo.UserQuestions.Count; i++) { %>
      <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions[i].UserQuestion)%>
      <%: Html.TextBoxFor(model => model.UserInfo.UserQuestions[i].UserAnswer)%>
    <% } %>
    

    那么至少你的代码是干的。

    [HttpPost公司] { IUserRepository repusser=新用户存储库(EntityFactory.GetEntity()); 用户usr=repUser.GetUserByID(Convert.ToInt32(Session[“UserID”]);

    UpdateModel(usr);
    EntityFactory.GetEntity().SaveChanges();
    

    }

    取决于问题是否存在,这应该有效。 UserInfo对象应该包含您的问题,您可以 foreach 并手动添加。

        5
  •  0
  •   Shiraz Bhaiji    14 年前