MVC3 @html.radiobuttonfor

2.9k views Asked by At

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 :

@model IEnumerable SQLOperation.Models.QuestionClass.Tabelfields


<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.List1[SQLOperation.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[SQLOperation.Models.QuestionClass+Tabelfields]'."

Why should I get this error and how can I solve this ?

Thank you,

ajay

2

There are 2 answers

8
Gaz Winter On

You need to change:

  @model IEnumerable SQLOperation.Models.QuestionClass.Tabelfields

to this:

  @model List<SQLOperation.Models.Question>

That should fix the error.

Hope it helps.

0
Karthik Chintala On

Change your getallQuestion in model to this

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

            return list.AsEnumerable();  //just remove `ToList()` and place `AsEnumerable()`.
        } 

Try this. This should work for you.