Setp By setp working on MVC3 razor @html.radiobuttonfor controll.
Can someone suggest me book, project, any link which will give me detail about MVC3 @html.radiobuttonfor controller ?
I also want to create Question answer feedback form which gives me one question and it's four option using radiobutton.
@model IEnumerable<SQLOperation.Models.Question>
@{
ViewBag.Title = "Question";
}
<h2>Question</h2>
@{string[] a = new string[Model.Count()]; int i = 0; }
@foreach (var item in Model)
{
using (Html.BeginForm("Question", "home", new {Email=item.Email}))
{
@Html.DisplayFor(modelItem => item.QuestionName)
<br /><br />
if (item.Option1 != "")
{
@Html.RadioButtonFor(modelItem => a[i], item.Option1,item)
@Html.DisplayFor(modelItem => item.Option1)
<br /><br />
}
if (item.Option2 != "")
{
@Html.RadioButtonFor(modelItem => a[i], item.Option2)
@Html.DisplayFor(modelItem => item.Option2)
<br /><br />
}
if (item.Option3 != "")
{
@Html.RadioButtonFor(modelItem => a[i], item.Option3)
@Html.DisplayFor(modelItem => item.Option3)
<br /><br />
}
if (item.Option4 != "")
{
@Html.RadioButtonFor(modelItem => a[i], item.Option4)
@Html.DisplayFor(modelItem => item.Option4)
<br /><br />
}
i = (Int16)i + 1;
if (Model.Count() == i)
{
<input name="btnsumbit" type="submit" value="Submit Feedback"
style="font-family:Segoe UI Light;font-size:medium;"/>
}
}
}
My Controller
public ActionResult Question(string email)
{
return View(QuestionClass.getallQuestion(email));
}
[HttpPost, ActionName("Question")]
public void Question(string[] a, int Id,string Email, QuestionClass.Tabelfields tf)
{
}
I didn't get the question and it's selected option with appropriate Email in controller . How can get it in controller?
It is a simple Question and answer form created by a particular person who have his Email.
Thank you for your reply. I am sorry I am new to mvc and did't understand much about Group collection. Can you please elaborate it?
Basically what I am trying is to create a feedback form with "a question which has four answers (Radio Button) using MVC."
In View: I am able to get questions and answers in view with radiobutton to each answer.
In Controller: I am confused here.
I would like to send selected question & there answers from View to controller.
I have created a table in SQL which has questions and Answers. (@model IEnumerable<SQLOperation.Models.Question>). I
have pasted my view and controller code in earlier post. Is there anything that I need to change in controller code?
public class QuestionClass
{
public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext();
public class Tabelfields : Question
{
//public int QuestionID { get; set; }
//public string Email { get; set; }
//public string QuestionName { get; set; }
//public string Option1 { get; set; }
//public string Option2 { get; set; }
//public string Option3 { get; set; }
//public string Option4 { get; set; }
public string SelectedOption { get; set; }
}
public static List<Question> getallQuestion(string email)
{
var list = (from q in context.Questions where q.Email==@email select q).ToList();
return list.ToList();
}
}
public ActionResult Question(string email)
{
return View(QuestionClass.getallQuestion(email));
}
[HttpPost, ActionName("Question")]
public void Question(string Email,List<QuestionClass.Tabelfields> q)
{
}
In class i.e. "Tabelfields" I create new property i.e. SelectedOption. I inherite the base class i.e. Question. Where Question is a table in Sql server Database.
"The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[SQLOperation.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SQLOperation.Models.QuestionClass+Tabelfields]'."
Why should I get this error and how can I solve this ?
This line is returning List of Question and not the List<QuestionClass.Tabelfields>
and thats the reason it is failing, either you convert your question class into other type or can add the additional field to have selection in the Question itself and that should work.
ajaypunekar1
0 Points
9 Posts
MVC3 @html.radiobuttonfor
Aug 18, 2012 10:40 AM|LINK
Setp By setp working on MVC3 razor @html.radiobuttonfor controll.
Can someone suggest me book, project, any link which will give me detail about MVC3 @html.radiobuttonfor controller ?
I also want to create Question answer feedback form which gives me one question and it's four option using radiobutton.
How do I use @html.RadioButtonFor inside foreach?
Thank you,
ajay
RAZOR mvc3
CPrakash82
All-Star
18256 Points
2837 Posts
Re: MVC3 @html.radiobuttonfor
Aug 18, 2012 01:56 PM|LINK
You can use the below code, may require some modification or look for the radio button list here.
@foreach(var answer in model.answers)
{
Html.RadioButtonFor(m=>m.SelectedAnswer, answer , [default value])
}
RAZOR mvc3
ajaypunekar1
0 Points
9 Posts
Re: MVC3 @html.radiobuttonfor
Aug 20, 2012 06:37 AM|LINK
Here is my view
@model IEnumerable<SQLOperation.Models.Question> @{ ViewBag.Title = "Question"; } <h2>Question</h2> @{string[] a = new string[Model.Count()]; int i = 0; } @foreach (var item in Model) { using (Html.BeginForm("Question", "home", new {Email=item.Email})) { @Html.DisplayFor(modelItem => item.QuestionName) <br /><br /> if (item.Option1 != "") { @Html.RadioButtonFor(modelItem => a[i], item.Option1,item) @Html.DisplayFor(modelItem => item.Option1) <br /><br /> } if (item.Option2 != "") { @Html.RadioButtonFor(modelItem => a[i], item.Option2) @Html.DisplayFor(modelItem => item.Option2) <br /><br /> } if (item.Option3 != "") { @Html.RadioButtonFor(modelItem => a[i], item.Option3) @Html.DisplayFor(modelItem => item.Option3) <br /><br /> } if (item.Option4 != "") { @Html.RadioButtonFor(modelItem => a[i], item.Option4) @Html.DisplayFor(modelItem => item.Option4) <br /><br /> } i = (Int16)i + 1; if (Model.Count() == i) { <input name="btnsumbit" type="submit" value="Submit Feedback" style="font-family:Segoe UI Light;font-size:medium;"/> } } }My Controller
public ActionResult Question(string email) { return View(QuestionClass.getallQuestion(email)); } [HttpPost, ActionName("Question")] public void Question(string[] a, int Id,string Email, QuestionClass.Tabelfields tf) { }I didn't get the question and it's selected option with appropriate Email in controller . How can get it in controller?
It is a simple Question and answer form created by a particular person who have his Email.
Thank you,
Ajay
CPrakash82
All-Star
18256 Points
2837 Posts
Re: MVC3 @html.radiobuttonfor
Aug 20, 2012 11:07 AM|LINK
You can change your view code to use EditorFor Templates and it will make your life easier.
Your View Code-
@model IEnumerable<SQLOperation.Models.Question> @{ ViewBag.Title = "Question"; } <h2>Question</h2> @using (Html.BeginForm("Question", "home")) { Html.EditorFor(x=> x.Questions); <input name="btnsumbit" type="submit" value="Submit Feedback" style="font-family:Segoe UI Light;font-size:medium;"/> }ajaypunekar1
0 Points
9 Posts
Re: MVC3 @html.radiobuttonfor
Aug 20, 2012 01:06 PM|LINK
Hi Prakash,
Thank you for your reply. I am sorry I am new to mvc and did't understand much about Group collection. Can you please elaborate it?
Basically what I am trying is to create a feedback form with "a question which has four answers (Radio Button) using MVC."
In View: I am able to get questions and answers in view with radiobutton to each answer.
In Controller: I am confused here.
I would like to send selected question & there answers from View to controller.
I have created a table in SQL which has questions and Answers. (@model IEnumerable<SQLOperation.Models.Question>). I have pasted my view and controller code in earlier post. Is there anything that I need to change in controller code?
Thanks for your help.
Radiobutton mvc3
CPrakash82
All-Star
18256 Points
2837 Posts
Re: MVC3 @html.radiobuttonfor
Aug 21, 2012 12:03 AM|LINK
Alright, I took it too far.
Modify your Question Model class to have one property for selected answer, then you need to use that property to bind with RadioButton like below.
@Html.RadioButtonFor(modelItem => a[i], item.Option1,item) or your other radio buttons will be replaced with
@Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option1, item)
@Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option2)
@Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option3)
@Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option4)
Once user select any radio button and submit the form you will get the selected value in SelectedOption property.
ajaypunekar1
0 Points
9 Posts
Re: MVC3 @html.radiobuttonfor
Aug 21, 2012 06:02 AM|LINK
Hi Prakash,
Thank you for reply.
I have made changes according to your suggestion.
Here is my class :
public class QuestionClass { public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext(); public class Tabelfields : Question { //public int QuestionID { get; set; } //public string Email { get; set; } //public string QuestionName { get; set; } //public string Option1 { get; set; } //public string Option2 { get; set; } //public string Option3 { get; set; } //public string Option4 { get; set; }public string SelectedOption { get; set; } } public static List<Question> getallQuestion(string email) { var list = (from q in context.Questions where q.Email==@email select q).ToList(); return list.ToList(); } }My view after Changes :
@model IEnumerable<SQLOperation.Models.QuestionClass.Tabelfields> @{ ViewBag.Title = "Question"; } <p> Question</p> @foreach (var item in Model) { using (Html.BeginForm("Question", "home", new {email=item.Email,item})) { @Html.DisplayFor(modelItem => item.QuestionName) <br /><br /> if (item.Option1 != "") { @Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option1, item) @Html.DisplayFor(modelItem => item.Option1) <br /><br /> } if (item.Option2 != "") { @Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option2) @Html.DisplayFor(modelItem => item.Option2) <br /><br /> } if (item.Option3 != "") { @Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option3) @Html.DisplayFor(modelItem => item.Option3) <br /><br /> } if (item.Option4 != "") { @Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option4) @Html.DisplayFor(modelItem => item.Option4) <br /><br /> } i = (Int16)i + 1; if (Model.Count() == i) { <input name="btnsumbit" type="submit" value="Submit Feedback" style="font-family:Segoe UI Light;font-size:medium;"/> } } }Here Is my controller
public ActionResult Question(string email) { return View(QuestionClass.getallQuestion(email)); } [HttpPost, ActionName("Question")] public void Question(string Email,List<QuestionClass.Tabelfields> q) { }In class i.e. "Tabelfields" I create new property i.e. SelectedOption. I inherite the base class i.e. Question. Where Question is a table in Sql server Database.
I create strongely type view by using this
@model IEnumerable<SQLOperation.Models.Question>
If I change strongely type view as
@model IEnumerable<SQLOperation.Models.QuestionClass.Tabelfields>
I get this error
"The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[SQLOperation.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SQLOperation.Models.QuestionClass+Tabelfields]'."
Why should I get this error and how can I solve this ?
Thank you,
ajay
CPrakash82
All-Star
18256 Points
2837 Posts
Re: MVC3 @html.radiobuttonfor
Aug 21, 2012 10:57 AM|LINK
This line is returning List of Question and not the List<QuestionClass.Tabelfields> and thats the reason it is failing, either you convert your question class into other type or can add the additional field to have selection in the Question itself and that should work.
ajaypunekar1
0 Points
9 Posts
Re: MVC3 @html.radiobuttonfor
Aug 21, 2012 11:18 AM|LINK
I solved this problem like this
[HttpGet] public ActionResult Question(string email) { var tf = new QuestionClass.Tabelfields(); IList<QuestionClass.Tabelfields> viewmodel = new List<QuestionClass.Tabelfields>(); var q = QuestionClass.getallQuestion(email).ToList(); foreach (SQLOperation.Models.Question item in q) { QuestionClass.Tabelfields viewItem = new QuestionClass.Tabelfields(); viewItem.Email = item.Email; viewItem.QuestionID = item.QuestionID; viewItem.QuestionName = item.QuestionName; viewItem.Option1 = item.Option1; viewItem.Option2 = item.Option2; viewItem.Option3 = item.Option3; viewItem.Option4 = item.Option4; viewmodel.Add(viewItem); } return View(viewmodel); }Now the main task is to return list of selected selected answer (RadioButton) to controller.
Here is my View
@model IEnumerable<SQLOperation.Models.QuestionClass.Tabelfields> @{ ViewBag.Title = "Question"; } <p> Question</p> @{int i = 0;} @foreach (var item in Model) { using (Html.BeginForm("Question", "home", new {email=item.Email,option = item.SelectedOption})) { @Html.DisplayFor(modelItem => item.QuestionName,item) <br /><br /> if (item.Option1 != "") { @Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option1, item) @Html.DisplayFor(modelItem => item.Option1) <br /><br /> } if (item.Option2 != "") { @Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option2,item) @Html.DisplayFor(modelItem => item.Option2) <br /><br /> } if (item.Option3 != "") { @Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option3,item) @Html.DisplayFor(modelItem => item.Option3) <br /><br /> } if (item.Option4 != "") { @Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option4,item) @Html.DisplayFor(modelItem => item.Option4) <br /><br /> } i = (Int16)i + 1; if (Model.Count() == i) { <input name="btnsumbit" type="submit" value="Submit Feedback" style="font-family:Segoe UI Light;font-size:medium;"/> } } }My controller
[HttpPost, ActionName("Question")] public void Question(string Email,QuestionClass.Tabelfields item,string option) { }How can I get all selected answer with appropriate question to controller?
thank you for rply,
CPrakash82
All-Star
18256 Points
2837 Posts
Re: MVC3 @html.radiobuttonfor
Aug 21, 2012 12:23 PM|LINK
[HttpPost, ActionName("Question")]
public void Question(IEnumerable<QuestionClass.Tabelfields> items)
{
}
Your method signature should match per view.