【问题标题】:IEnumerable' does not contain a definition for 'Type'IEnumerable' 不包含 'Type' 的定义
【发布时间】: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&lt;Leading&gt;,但消息清楚地告诉您它的Leading(不是IEnumerable)。如果它是@model IEnumerable&lt;Leading&gt;,那么您在&lt;form&gt; 中的代码将引发其他错误。您没有向我们显示正确的代码
  • @CraigW.,那会抛出 this error
  • 事实上,消息指出模型是LeadingSires(这与您显示的代码完全无关)
  • @CraigW.,我已经添加了控制器。请看一看。谢谢。

标签: asp.net-mvc


【解决方案1】:

发生错误是因为您视图中的模型是IEnumerable&lt;Leading&gt;,但您尝试为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>

【讨论】:

  • 我添加的Leading类怎么样。我应该更改公共字符串类型 { get;放; } 进入公共 int?输入 {get;set;}
  • 再给我 10 分钟,我会在聊天中解释这一点以及其他一些问题 :)
  • 当然。请慢慢来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
相关资源
最近更新 更多