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

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

问题描述

我已经决定了单选按钮矩阵的MVC视图。单选按钮的每一行是一个组,再presents从模型类型化的对象。使用的各种博客和发布指导我已成功绑定的发布表单结果到控制器动作的类型化模式阵列,但似乎无法成功扭转效应和现有模型绑定单选按钮,而preserving他们的选择或者未选中状态。

我的模型中含有一种叫AnswerValue的属性是0到4之间,并应与单选按钮的名称匹配。我试过指数值更改为模型值AnswerId但这样做的结合这是工作不再作品(我相信索引必须从零开始)。下面是到目前为止,我已经使用这个发表一些资源和<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow>文章由Scott Hanselman在来得到我在现在的位置。

如果任何人有关于如何执行这种双向绑定这将是更AP preciated任何见解。

感谢

我的控制器:

 的[AcceptVerbs(HttpVerbs.Post)
  公众的ActionResult保存([装订(preFIX =SurveyAnswer)] SurveyAnswer []的反应,诠释SurveyID)
  {

我的观点:

 &LT;%
  INT questionIndex = 0;
  的foreach(在型号SurveyAnswer Q)
  {%GT;
  &所述;%= Html.Hidden(SurveyAnswer.Index,questionIndex)%GT;
  &LT;%= Html.Hidden(SurveyAnswer [+ questionIndex +。AnswerIdq.AnswerId)%GT;
  &所述; TR&GT;
    &LT; TD风格=背景色:#AAAAAA;填充左:10px的;填充右:10px的;右边框:1px的固体#fffff;'&GT;&LT;%= questionIndex + 1%GT;&LT; / TD&GT;
    &LT; TD风格=文本对齐:权利;'&GT;&LT;%= q.Question.DisplayValue%GT;&LT; / TD&GT;
    &所述; TD&GT;&下;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,0,新的{名称=SurveyAnswer [+ questionIndex +] .AnswerValue})%GT; &LT; / TD&GT;
    &所述; TD&GT;&下;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,1,新的{名称=SurveyAnswer [+ questionIndex +] .AnswerValue})%GT; &LT; / TD&GT;
    &LT; TD&GT;&LT;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,2,新{NAME =SurveyAnswer [+ questionIndex +] .AnswerValue})%&GT; &LT; / TD&GT;
    &LT; TD&GT;&LT;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,3,新{NAME =SurveyAnswer [+ questionIndex +] .AnswerValue})%&GT; &LT; / TD&GT;
    &LT; TD&GT;&LT;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,4,新{NAME =SurveyAnswer [+ questionIndex +] .AnswerValue})%&GT; &LT; / TD&GT;
  &LT; / TR&GT;
&LT;%
    questionIndex ++;
  }
%GT;


解决方案

单选按钮本身不能识别的值(在状态方面)比真/假等。
所以,你必须将值与实际位置比较和明确设置TRUE / ​​FALSE状态。

尝试用这个重载的单选按钮帮手。

 &LT;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,0,q.AnswerValue == 0)%&GT;
&所述;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,1,q.AnswerValue == 1)%GT;
&所述;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,2,q.AnswerValue == 2)%GT;
&所述;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,3,q.AnswerValue == 3)%GT;
&所述;%= Html.RadioButton(SurveyAnswer [+ questionIndex +] .AnswerValue,4,q.AnswerValue == 4)%GT;

如果您使用的是视图模型,也许是更好地把这种逻辑问题( q.AnswerValue == 4 ),在那里,而不是直接在视图中。

PS:我觉得可以去掉最后一个参数作为第一个已经设置ID和名称atributes为: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天全站免登陆