【发布时间】:2010-10-31 21:31:50
【问题描述】:
这是一个常见的场景
我有这些实体
用户
用户ID
用户名
...
用户问题
用户QID
用户ID
用户问题
用户回答
一个用户可以有很多问题,但一个问题附加到一个用户。
在我的示例中,我创建了 3 个空实体 (UserQuestion)。然后我将这些问题附在用户身上。
在视图中,对于每个问题、答案文本框,我使用 ElementAt 来指定每个 UserQuestion Entity。
为什么我不能保存这三个问题
在控制器中
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)%>
3
【问题讨论】:
标签: asp.net-mvc entity-framework asp.net-mvc-2 entity-framework-4 entity-relationship