ASP.NET MVC使用类型模型的模型到单选按钮列表的双向数据绑定 [英] ASP.NET MVC Two Way Data Binding of Model to Radio Button List using Typed Model

查看:95
本文介绍了ASP.NET MVC使用类型模型的模型到单选按钮列表的双向数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mvc视图,由一个矩阵的单选按钮组成。每行单选按钮都在一个组中,表示来自模型的类型化对象。使用各种博客和帖子的指导,我已成功地将发布的表单结果绑定到控制器操作中的类型模型数组,但是似乎不能成功地将效果反转并将现有模型绑定到单选按钮,同时保留其选择或未选择的状态



我的模型包含一个名为AnswerValue的属性,该属性介于0到4之间,并且应与radiobutton名称匹配。我尝试将索引值更改为模型值AnswerId,但是这样做的工作不再有效(我相信索引必须为零)。以下是我迄今为止使用的一些资源,这个发布 Scott Hanselman的文章,以获得我在哪里现在。



如果任何人有任何关于如何执行这种双向绑定的洞察力,将不胜感激。



感谢



我的控制器:

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

我的观点:

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


解决方案

单选按钮本身不识别值除了真/假之外的状态)。
因此,您必须将值与实际位置进行比较,并显式设置TRUE / FALSE状态。



尝试使用单选按钮帮助程序的此重载。

 <%= 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)%>

如果您使用ViewModel,也许最好把这个逻辑问题( q.AnswerValue == 4 ),而不是直接在视图中。



PS:我想你可以删除最后一个参数第一个已将ID和名称设置为:SurveyAnswer [+ questionIndex +] .AnswerValue


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

解决方案

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"

这篇关于ASP.NET MVC使用类型模型的模型到单选按钮列表的双向数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆