2

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?

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
Ajay
  • 6,418
  • 18
  • 79
  • 130
  • OK public ActionResult Question(string email) { var tf = new QuestionClass.Tabelfields(); IList viewmodel = new List(); 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; viewmodel.Add(viewItem); } return View(viewmodel); } – Ajay Aug 21 '12 at 11:52

1 Answers1

1

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.

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • Thank you for rply. I have made changes in view and also in controller like this using (Html.BeginForm("Question", "home", new { email = item.Email, qid = item.QuestionID,qname=item.QuestionName ,option=item.SelectedOption})) my Controller public void Question(string Email, int qid,string qname,string option) when i run this i get email,qid,qname but not get option selected of one question. can u tell what changes i have to do in @html.beginform and in controller? thank you – Ajay Aug 21 '12 at 12:30
  • Thank you for reply, how can I return the select radiobutton with appropriate answer to controller?? I have change this but I am not getting values in controller – Ajay Aug 21 '12 at 13:16
  • 1
    problem solved @for (int row = 0; row < Model.Count; row++) { @Html.EditorFor(x => x[row].FirstName ) } Thank you for help – Ajay Aug 24 '12 at 06:06