MVC3 @html.radiobuttonfor

两盒软妹~` 提交于 2019-12-20 03:25:12

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12051487/mvc3-html-radiobuttonfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!