views:

59

answers:

1

Here is a common scenario
I Have these Entity
User
UserId
UserName
...

UserQuestion
UserQID
UserID
UserQuestion
UserAnswer


When the user first logon, he need to create 3 customs Answer / question. How can I Create these 3 questions relation and save it to database. I Want these questiona in relation with the specific user.


In the Controller

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

In my View

<%: 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