razor - binding issue with radio button

前端 未结 1 941
耶瑟儿~
耶瑟儿~ 2021-01-28 02:10

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 sett

1条回答
  •  时光说笑
    2021-01-28 03:01

    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 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 PossibleAnswers { get; set; }
    

    and in the controller

    model.Questions[0].PossibleAnswers = new List() { "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)
        {
          
        }
      }
      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.

    0 讨论(0)
提交回复
热议问题