Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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++; 
  }
%>
like image 547
Jeremy Avatar asked Dec 09 '09 15:12

Jeremy


1 Answers

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"

like image 168
JOBG Avatar answered Sep 21 '22 17:09

JOBG