MVC3 @Html.RadioButtonfor Pass data from view to controller

4.6k views Asked by At

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.

My view :

@model  IEnumerable SQLOperation.Models.QuestionClass.Tabelfields


@{int i = 0;}


     @foreach (var item in Model)
     {
     using (Html.BeginForm("Question", "home", new {email=item.Email}))
     {   

         @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)              
                    @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;"/>
         }
     }
 }

My Controller :

 [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);
    }

    [HttpPost, ActionName("Question")]
    public void Question(string Email,QuestionClass.Tabelfields tf)
    {

    }

My Model:

public class QuestionClass

{

  public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext();
    public class Tabelfields : Question
    {
        //public decimal 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 int SelectedOption { get; set; }
    }


    public static List<Question> getallQuestion(string email)
    {
        var list = (from q in context.Questions where q.Email == @email select q);

        return list.ToList();
    }

}

I didn't get the question and it's selected option with appropriate Email in controller.

How can I get it in controller?

1

There are 1 answers

3
Yasser Shaikh On

To, start with include a breakpoint in your view and check if the values are being passed or not.

Next, change your method signature from

 public void Question(string Email,QuestionClass.Tabelfields tf)

to

 public void Question(string email, QuestionClass.Tabelfields tf)

Also, I see you have used some strange syntax ( considering m also new to MVC ) where you could have simply used this

@Html.DisplayFor(modelItem => item.QuestionName)

instead of this

@Html.DisplayFor(modelItem => item.QuestionName,item)

and also, I have seen you profile, I know you are new to StackOverflow, so Welcome to StackOverflow ! you might want to read this & this as well. Cheers !

Update :

You are using this for displaying a radio button...

@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option1, item)

what is item.Option1, I can see you have commented out Opion1,2,3,4. Or are you still using them ?

A very simple way of implementing this would be something like this

@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option1")
@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option2")
@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option3")

or you can also use,

@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option1)
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option2)
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option3)

provided values exist in item.Option1/2/3/4 and are all of them are different.