【发布时间】:2018-05-21 00:07:44
【问题描述】:
我正在尝试使用此@Html.DropDownListFor 来过滤结果。
控制器:
[HttpGet]
public ActionResult Leading()
{
ReportClassHandle ReportClassHandle = new ReportClassHandle();
return View(ReportClassHandle.LeadingAll());
}
而LeadingAll的方法是:
public List<Leading> LeadingAll(string Type)
{
clsUtilities clsUtilities = new clsUtilities();
DataSet ds;
List<Leading> leading = new List<Leading>();
string sSQL;
SqlParameter[] prms = new SqlParameter[1];
sSQL = "exec GetLeading @Type";
prms[0] = new SqlParameter("@Type", SqlDbType.VarChar);
prms[0].Value = Type;
ds = clsUtilities.CreateCommandwithParams(sSQL, prms);
DataTable dataTable = ds.Tables[0];
foreach(DataRow dr in dataTable.Rows)
{
leading.Add(new Leading
{
RankId = Convert.ToInt32(dr["RankId"]),
Name = Convert.ToString(dr["Name"]),
});
}
我完整的Leading View 是:
@model IEnumerable<ReportProject.Models.Leading>
@using (Html.BeginForm("Leading", "Home", FormMethod.Post))
{
@Html.DisplayFor(m => m.Type)
@Html.DropDownListFor(m => m.Type, new List<SelectListItem> {
new SelectListItem{ Text="General", Value="1" },
new SelectListItem{Text="Advance", Value="2"}}, "Please select")
@Html.ValidationMessageFor(m => m.Type, "", new { @class = "error" })
}
<table class="table table-striped">
<tr>
<th>@Html.DisplayName("Rank")</th>
<th>@Html.DisplayName("Name")</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.RankId)</td>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
</tr>
}
</table>
我的Leading Model:
public class Leading
{
public int RankId { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "Type is Required")]
public string Type { get; set; }
}
当我运行这段代码时,它会抛出错误:
编译器错误消息:CS1061:“IEnumerable
”不包含“Type”的定义,并且找不到接受“IEnumerable ”类型的第一个参数的扩展方法“Type”(您是否缺少using 指令还是程序集引用?)
请指导我。
【问题讨论】:
-
请显示返回此视图的控制器代码。我的猜测是它没有返回模型类型的 IEnumerable。
-
您所声称的根本不可能。您在视图中的模型是
@model IEnumerable<Leading>,但消息清楚地告诉您它的Leading(不是IEnumerable)。如果它是@model IEnumerable<Leading>,那么您在<form>中的代码将引发其他错误。您没有向我们显示正确的代码 -
@CraigW.,那会抛出 this error
-
事实上,消息指出模型是
LeadingSires(这与您显示的代码完全无关) -
@CraigW.,我已经添加了控制器。请看一看。谢谢。
标签: asp.net-mvc