IEnumerable的'不包含' Type'的定义

时间:2017-12-07 00:24:15

标签: asp.net-mvc

我正在尝试使用此@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:&#39; IEnumerable&lt; Leading&gt;&#39;不包含&#39; Type&#39;的定义没有扩展方法&#39; Type&#39;接受类型&#39; IEnumerable&lt; Leading&gt;&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

请指导我。

1 个答案:

答案 0 :(得分:1)

发生错误是因为视图中的模型是IEnumerable<Leading>,但您尝试为Leading的单个实例创建控件。由于您希望使用下拉列表的值来过滤结果,因此您应该从视图模型开始,以在视图中表示您想要的内容

public class LeadingFilterVM
{
    public int? Type { get; set; }
    public IEnumerable<SelectListItem> TypeList { get; set; }
    public IEnumerable<Leading> Leadings { get; set; }
}

那么你的GET方法将是

[HttpGet]
public ActionResult Leading(int? type)
{
    IEnumerable<Leading> data;
    ReportClassHandle ReportClassHandle = new ReportClassHandle();
    if (type.HasValue)
    {
        data = ReportClassHandle.LeadingAll(type.Value.ToString())
    }
    else
    {
        data = ReportClassHandle.LeadingAll();
    }
    LeadingFilterVM model = new LeadingFilterVM
    {
        Type = type,
        TypeList = new List<SelectListItem>
        {
            new SelectListItem{ Text = "General", Value = "1" }, 
            new SelectListItem{ Text = "Advance", Value = "2" }
        },
        Leadings = data
    };
    return View(model);
}

将视图更改为

@model LeadingFilterVM
// note the FormMethod.Get
@using (Html.BeginForm("Leading", "Home", FormMethod.Get))
{
    @Html.DisplayFor(m => m.Type) 
    @Html.DropDownListFor(m => m.Type, Model.TypeList "All types")

    <input type="submit" value="search" />
}
<table class="table table-striped">
    <thead>
        <tr>
            <th>Rank</th>
            <th>Name</th>   
            <th></th>
        </tr>
    <thead>
    <tbody>
        @foreach (var item in Model.Leadings)
        {
            <tr>
                <td>@Html.DisplayFor(m => item.RankId)</td>
                <td>@Html.DisplayFor(m => item.Name)</td>
             </tr>
        }
    </tbody>
</table>