剃须刀 - 无线电按钮绑定问题 [英] razor - binding issue with radio button

查看:120
本文介绍了剃须刀 - 无线电按钮绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我奋力绑定ANS的单选按钮,我在做一个小项目。
我有两个问题和答案模式。我想才达到的拉动设置用户将有多个选择,或键入文本框中答题和基地的名单是什么。与单选按钮我似乎无法俺们绑定歌厅空对象可有人请点我在正确的方向。

  @model名单< Question.Models.Questionnaire>
@ {
ViewBag.Title =指数;
布局=〜/查看/共享/ _Layout.cshtml
}< H2>问题列表< / H>@using(Html.BeginForm(的getAnswer,家)){
的for(int i = 0; I< Model.Count;我++){    <文字和GT; @Model [I] .Questions< /文字和GT; < BR />    @ Html.HiddenFor(M => M [​​I] .QuestionID)    如果(@Model [I] .MultipleChoice == FALSE){        @ Html.TextBoxFor(M => M [​​I] .Response)LT; BR />    }其他{          对于(INT J = 0; J<产品型号[I] .GetAns.Count; J ++){
               < D​​IV>
              <文字和GT; @Model [I] .GetAns [J] .AnsText< /文字和GT;
              @ Html.RadioButtonFor(M =>产品型号[I] .QuestionID,型号[I] .GetAns [J] .AnswerId)LT; BR />
               < / DIV>
        }        / *
        的foreach(在PossibleAns VAR ansOption){
          < D​​IV>
           @ Html.RadioButtonFor(型号=>产品型号[I] .QuestionID,ansOption.AnswerId)
           @ Html.Label(ansOption.AnsText)
          < / DIV>
        }
         * * /
        < BR />
    }
}
< BR />  <输入类型=提交名称=提交/>

}

 公共类问卷
{
    公众问卷(){
    }    公众诠释QuestionID {搞定;组;}
    公共字符串名称{搞定;组;}
    公共字符串问题{搞定;组;}
    公共字符串响应{搞定;组;}
    公共BOOL MultipleChoice {搞定;组;}    公开名单<应答和GT; GetAns {设置;得到;}} 公共类答
 {
    公众诠释AnswerId {搞定;组; }
    公共字符串AnsText {获取;设置;}
    公众问卷QUES {搞定;组;}
 }
公共类QuestionRepository
{    公开名单<&问卷GT;
         GetQuestionnaire(){
             清单<&问卷GT; Q =新的List<&问卷GT;();
             q.Add(新问卷(){
                 QuestionID = 11,标题=杰奥,问题=英格兰的首都?,GetAns = GetAns(),MultipleChoice = TRUE
             });
             q.Add(新问卷(){
                 QuestionID = 22,标题=杰奥,问题=法国之都,GetAns = GetAns()
             });
             q.Add(新问卷(){
                 QuestionID = 33,标题=杰奥,问题=古巴之都,GetAns = GetAns()
             });
             返回q;
    }    公开名单<应答和GT; GetAns(){        清单<应答和GT; ANS =新的List<应答和GT;();
        ans.Add(新答案(){
            AnswerId = 1,AnsText =伦敦,疑问句=新问卷(){
                QuestionID = 11
            }
        });
        ans.Add(新答案(){
            AnswerId = 2,AnsText =巴黎,疑问句=新问卷(){
                QuestionID = 22
            }
        });
        ans.Add(新答案(){
            AnswerId = 3,AnsText =哈瓦那,疑问句=新问卷(){
                QuestionID = 33
            }
        });        返回答;
    }}


解决方案

您单选按钮绑定属性 QuestionID (和你的覆盖 QuestionID 选择 AnswerID 所以,当你回来后没有什么是有意义的,你的类和属性命名使得它很难理解,但如果你想从一组可能的答案中的一个答案,那么财产 GetAns 只是需要列表<串> 然后绑定所选择的答案财产响应。例如,对于第一个问题(的英格兰的首都?的),你似乎是想的选择是的伦敦巴黎哈瓦那的,那么问题应该有一个属性。

 公开名单<串GT; PossibleAnswers {搞定;组; }

和在控制器

  model.Questions [0] = .PossibleAnswers新的List<串GT;(){伦敦,巴黎,哈瓦那}

和视图

 的for(int i = 0; I< Model.Count;我++){
{  如果(@Model [I] .MultipleChoice)
  {
    //为选择题单选按钮
    的foreach(在型号字符串选项[I] .PossibleAnswers)
    {
      <标签>
        @ Html.RadioButtonFor(M = GT; M [​​] .Response,选择冲)
        <跨度> @选项< / SPAN>
      < /标签>
    }
  }
  其他
  {
    //为文本框答案
    @ Html.TextBoxFor(M = GT; M [​​] .Response)
  }
}

当你回来后,在响应属性将包含答案的文字,无论是在文本框中输入了,或者它的一个可能的答案之一是什么选择题。

不过,我建议你需要重新考虑大部分你做什么在这里,特别是如果你想绑定到一个选择题答案的ID属性。

I am struggling to bind ans for radio button i am doing a small project. I have two model Question and Answer. What i want to achive for pull a list of question and base on setting the user will have multiple choice or type the answer in a text box. with the radio button i cant seem to bind the ans it geting a null object can someone please point me in the right direction.

@model List<Question.Models.Questionnaire>


@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Question List</h2>

@using(Html.BeginForm("GetAnswer","Home")) {


for(int i = 0;i < Model.Count;i++) {

    <text>@Model[i].Questions</text> <br />

    @Html.HiddenFor(M => M[i].QuestionID)

    if (@Model[i].MultipleChoice == false){

        @Html.TextBoxFor(M => M[i].Response) <br />

    } else {

          for(int j = 0;j < Model[i].GetAns.Count;j++) {
               <div>
              <text>@Model[i].GetAns[j].AnsText</text>
              @Html.RadioButtonFor(M => Model[i].QuestionID, Model[i].GetAns[j].AnswerId) <br />
               </div>
        }

        /*
        foreach(var ansOption in PossibleAns) {
          <div>
           @Html.RadioButtonFor(model => Model[i].QuestionID, ansOption.AnswerId)
           @Html.Label(ansOption.AnsText)
          </div>  
        }
         * */
        <br />
    }
}
<br />

  <input type="submit" name="submit" />

}

public class Questionnaire
{
    public Questionnaire() {
    }

    public int QuestionID  { get; set;}
    public string Title    { get; set;}
    public string Questions{ get; set;}
    public string Response { get; set;}
    public bool MultipleChoice { get; set;}

    public List<Answer> GetAns { set; get;}

}

 public class Answer  
 {
    public int AnswerId { get; set; }
    public string AnsText { get;set;}
    public Questionnaire Ques { get; set;}
 }


public class QuestionRepository
{

    public List<Questionnaire>
         GetQuestionnaire() {
             List<Questionnaire> q = new List<Questionnaire>();
             q.Add(new Questionnaire() {
                 QuestionID = 11, Title = "Geo", Questions = "Capital of England?", GetAns = GetAns(), MultipleChoice = true
             });
             q.Add(new Questionnaire() {
                 QuestionID = 22, Title = "Geo", Questions = "Capital of France", GetAns = GetAns()
             });
             q.Add(new Questionnaire() {
                 QuestionID = 33, Title = "Geo", Questions = "Capital of Cuba", GetAns = GetAns()
             });
             return q;
    }

    public List<Answer> GetAns() {

        List<Answer> ans = new List<Answer>();
        ans.Add(new Answer() {
            AnswerId = 1, AnsText = "london", Ques = new Questionnaire() {
                QuestionID = 11
            }
        });
        ans.Add(new Answer() {
            AnswerId = 2, AnsText = "paris", Ques = new Questionnaire() {
                QuestionID = 22
            }
        });
        ans.Add(new Answer() {
            AnswerId = 3, AnsText = "Havana", Ques = new Questionnaire() {
                QuestionID = 33
            }
        });

        return ans;
    }



}

解决方案

Your radio buttons are binding to property QuestionID (and your overwriting the value of QuestionID with the selected AnswerID so nothing would make sense when you post back. Your class and property naming makes it a difficult to understand, but if your wanting to select one answer from a group of possible answers, then property GetAns just needs to be List<string> and then bind the selected answer to property Response. For example, for the first question ("Capital of England?") where you seem to be wanting the choices to be "london", "paris" or "Havana", then the question should have a property

public List<string> PossibleAnswers { get; set; }

and in the controller

model.Questions[0].PossibleAnswers = new List<string>() { "london", "paris", "Havana" }

and in the view

for(int i = 0; i < Model.Count; i++) {
{

  if (@Model[i].MultipleChoice)
  {
    // Radio buttons for multiple choice
    foreach(string option in Model[i].PossibleAnswers)
    {
      <label>   
        @Html.RadioButtonFor(m => m[i].Response, option})
        <span>@option</span>
      </label>
    }
  }
  else
  {
    // Textbox for answer
    @Html.TextBoxFor(m => m[i].Response)
  }
}

When you post back, the Response property will contain the text of the answer, either what been entered in the text box, or one of the possible answers if its a multiple choice question.

However, I suggest you need to rethink most of what your doing here, especially if your wanting to bind to an ID property of a multiple choice answer.

这篇关于剃须刀 - 无线电按钮绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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