MVC3 @html.radiobuttonfor

时间:2012-08-21 09:01:49

标签: asp.net-mvc-3 radiobuttonfor

这是我的班级:

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

我的观点:

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

这是我的控制器

  public ActionResult Question(string email)

        {              
             return View(QuestionClass.getallQuestion(email)); 
        } 

        [HttpPost, ActionName("Question")] 
        public void Question(string Email,List<QuestionClass.Tabelfields> q) 
        { 
        }

在类中,即“Tabelfields”,我创建了新属性,即SelectedOption。我继承了基类,即问题。其中Question是Sql server Database中的表。

我使用此

创建强大的类型视图
@model IEnumerable SQLOperation.Models.Question

如果我改变强烈的类型视图

 @model IEnumerable SQLOperation.Models.QuestionClass.Tabelfields

我收到此错误

  

“传入字典的模型项的类型为'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]'。”

为什么我会收到此错误,我该如何解决?

谢谢,

的Ajay

2 个答案:

答案 0 :(得分:0)

您需要更改:

  @model IEnumerable SQLOperation.Models.QuestionClass.Tabelfields

到此:

  @model List<SQLOperation.Models.Question>

这应该可以解决错误。

希望它有所帮助。

答案 1 :(得分:0)

将模型中的getallQuestion更改为此

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()`.
        } 

试试这个。这应该适合你。