Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my View


@using (Html.BeginForm("updateFeedback", "Questionnaire", FormMethod.Post))
{
for (int i = 0; i< Model.Count(); i++)
{
Q @Html.DisplayFor(item => item[i].QuestionSeq). @Html.DisplayFor(item => item[i].Question)

var model = Model[i];

if (model.AnsType.Equals("Scale2"))
{
<label>@Html.RadioButton("Question" + model.QuestionSeq, 1)Much Better</label>
<label>@Html.RadioButton("Question" + model.QuestionSeq, 2)Better</label>
<label>@Html.RadioButton("Question" + model.QuestionSeq, 3)About the same</label>
<label>@Html.RadioButton("Question" + model.QuestionSeq, 4)Worse</label>
<label>@Html.RadioButton("Question" + model.QuestionSeq, 5)Much Worse</label>

}
if (model.AnsType.Equals("Scale6"))
{
&lt;label&gt;@Html.RadioButton("Question" + model.QuestionSeq, 1)Very Likely&lt;/label&gt;
&lt;label&gt;@Html.RadioButton("Question" + model.QuestionSeq, 2)Likely&lt;/label&gt;
&lt;label&gt;@Html.RadioButton("Question" + model.QuestionSeq, 3)Neutral&lt;/label&gt;
&lt;label&gt;@Html.RadioButton("Question" + model.QuestionSeq, 4)Unlikely&lt;/label&gt;
&lt;label&gt;@Html.RadioButton("Question" + model.QuestionSeq, 5)Very Unlikely&lt;/label&gt;

}
}
&lt;input type="submit" value="Submit" /&gt;
}

The values for the RadioButton is coming as null in the controller. I understand the syntax should be @html.RadiobuttonFor but i am unable to frame it in the current context.
Help will be appreciated.
Thanks in advance.
Posted
Comments
[no name] 4-Sep-15 3:17am    
@model Question

if (model.AnsType.Equals("Scale2"))
{
<label>@Html.RadioButtonFor( model => model.QuestionSeq, false, new { id = "TermsAndConditions_false" } )Much Better</label>
}

[HttpPost]
public ActionResult CreateAccount(Question question )
{
if (question.QuestionSeq)
{
//TODO: Other validation, and create the account.
}
}

Note: I have added demo code. Here I took Question as model so you can change it per your need.
TrishaC 4-Sep-15 5:38am    
if (model.AnsType.Equals("Scale2"))
{
<label>@Html.RadioButtonFor( model => model.QuestionSeq, false, new { id = "TermsAndConditions_false" } )Much Better</label>
}
the above block for me is a loop and the requirement is such that this block gets executed several times. As a result i have the same radiobutton name for all the radio buttons in the page (whichever uses the above five options).

1 solution

I have came across same scenario then implement below logic.Hope it will work for you

[HttpPost]
public ActionResult InterviewQuetionsAns(FormCollection FC)
{
InterviewDetails interviewdetails = new InterviewDetails();
int val;
foreach (string key in FC.Keys)
{
interviewdetails.InterviewID = (int)Session["Id"];
if (key.Contains("QIDIdR"))
{
interviewdetails.QID = Int32.TryParse(key.Replace("QIDIdR", "").Trim(), out val) ? val : 0;
interviewdetails.AnsID = Convert.ToInt32(FC[key]);
interviewdetails.AnsDesc = "0";

obj.InterviewDetails.Add(interviewdetails);
obj.SaveChanges();
}
else if (key.Contains("QIDIdT"))
{
interviewdetails.QID = Int32.TryParse(key.Replace("QIDIdT", "").Trim(), out val) ? val : 0;
interviewdetails.AnsID = 0;
interviewdetails.AnsDesc = FC[key];

obj.InterviewDetails.Add(interviewdetails);
obj.SaveChanges();
}

}

InterviewHDR interviewHDR = obj.InterviewHDR.First(v => v.InterviewID == (int)Session["Id"]);
interviewHDR.InterviewEndDate = DateTime.Now;
obj.SaveChanges();

return RedirectToAction("ThankYou");
}
 
Share this answer
 
Comments
TrishaC 4-Sep-15 8:41am    
I have used @Html.RadioButtonFor but i am still unable to fetch the value of Answer in my controller.
<label>@Html.RadioButtonFor(answer => model.Answer, "5", new { id = "Question" + model.QuestionSeq })Very Satisfied</label>
<label>@Html.RadioButtonFor(answer => model.Answer, "4", new { id = "Question" + model.QuestionSeq })Satisfied</label>
<label>@Html.RadioButtonFor(answer => model.Answer, "3", new { id = "Question" + model.QuestionSeq })Neither Satisfied nor Dissatisfied</label>
<label>@Html.RadioButtonFor(answer => model.Answer, "2", new { id = "Question" + model.QuestionSeq })Dissatisfied</label>
<label>@Html.RadioButtonFor(answer => model.Answer, "1", new { id = "Question" + model.QuestionSeq })Very Dissatisfied</label>

Is there something i am missing?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900