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

房东的猫 提交于 2019-12-21 15:39:07

问题


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++; 
  }
%>

回答1:


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"



来源:https://stackoverflow.com/questions/1874845/asp-net-mvc-two-way-data-binding-of-model-to-radio-button-list-using-typed-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!