这是我的课:
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 数据库中的一个表。
我用这个创建了强烈的类型视图
@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]’。”
为什么我会收到此错误,我该如何解决?
谢谢,
杰伊