ASP.NET MVC Two Way Data Binding of Model to Radio Button List using Typed Model
Asked Answered
L

1

6

I have a mvc view made up of a matrix of radio buttons. Each row of radio buttons is in a group and represents a typed object from the model. Using the guidance of various blogs and postings I have successfully bound the posted form results to the typed model array in the controller action, however cannot seem to successfully reverse the effect and bind an existing model to the radio buttons while preserving their selected or unselected state.

My model contains a property called "AnswerValue" which is between 0 and 4 and should match up with the radiobutton names. I tried changing the index value to the model value "AnswerId" but in doing so the binding that was working no longer works (I believe the index must be zero based). Here's a few resources I have used so far this Post and an Article by Scott Hanselman to get to where I am at now.

If anyone has any insight on how to perform this two way binding it would be much appreciated.

Thanks

My controller:

[AcceptVerbs(HttpVerbs.Post)]
  public ActionResult Save([Bind(Prefix = "SurveyAnswer")] SurveyAnswer[] responses, int SurveyID)
  {

My View:

<%
  int questionIndex = 0; 
  foreach (SurveyAnswer q in Model)
  {

%>
  <%=Html.Hidden("SurveyAnswer.Index", questionIndex)%>
  <%=Html.Hidden("SurveyAnswer["+questionIndex+"].AnswerId", q.AnswerId) %>
  <tr>
    <td style='background-color: #aaaaaa;padding-left: 10px; padding-right: 10px;border-right: solid 1px #fffff;'><%= questionIndex+1 %></td>
    <td style='text-align: right;'><%= q.Question.DisplayValue %></td>
    <td><%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "0", new { name = "SurveyAnswer[" + questionIndex + "].AnswerValue"})%></td>
    <td><%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "1", new { name = "SurveyAnswer[" + questionIndex + "].AnswerValue" })%></td>
    <td><%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "2", new { name = "SurveyAnswer[" + questionIndex + "].AnswerValue" })%></td>
    <td><%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "3", new { name = "SurveyAnswer[" + questionIndex + "].AnswerValue"})%></td>
    <td><%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "4", new { name = "SurveyAnswer[" + questionIndex + "].AnswerValue" })%></td>
  </tr>
<%
    questionIndex++; 
  }
%>
Lysine answered 9/12, 2009 at 15:56 Comment(0)
Q
6

The Radio Button itself doesn't recognize values(in terms of state) other than True/False. So you will have to compare the value with the actual position and explicitly set the TRUE /FALSE state.

Try with this overload of the radio button helper.

<%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "0", q.AnswerValue == 0)%>
<%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "1", q.AnswerValue == 1)%>
<%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "2", q.AnswerValue == 2)%>
<%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "3", q.AnswerValue == 3)%>
<%=Html.RadioButton("SurveyAnswer[" + questionIndex + "].AnswerValue", "4", q.AnswerValue == 4)%>

If you are using a ViewModel, maybe it's better to put this logic question (q.AnswerValue == 4) in there instead of directly in the view.

PS: I think you can remove the last parameter as the first already sets the ID and name atributes to : "SurveyAnswer[" + questionIndex + "].AnswerValue"

Quassia answered 9/12, 2009 at 16:43 Comment(2)
If I am using entity framework and the generated entity set's, what would be a good method for extending this to hold your recommended logic. Thanks for the help so far. Seems to work.Lysine
If you're not using a ViewModel pattern maybe is better to leave it like that, because it will involve changing some logic of the controller, the spectated model type at the view and maybe your validations procedures. The are mostly use when you have complex models(like 2 or more entity set's combined into a single form or to setup SelectList/RadioButtons/..), if you want o check out more of this pattern take a look at geekswithblogs.net/michelotti/archive/2009/10/25/… or the section "Using a ViewModel Pattern" by ScottGu in his ASP.NET MVC 1.0 Book.Quassia

© 2022 - 2024 — McMap. All rights reserved.