没有类型为“IEnumerable”的ViewData项具有键“x”。

时间:2018-02-02 19:11:06

标签: c# asp.net-mvc razor drop-down-menu view

在我看来,我遇到了问题,我的下拉列表失败了,不起作用,不知道为什么

@model DigikalaHR.View_Model.Entitys.Member
 .
 .
 .
 .
<div class="form-group">
        @Html.LabelFor(model => model.TeamId, "TeamId", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">                
            @Html.DropDownList("TeamId",null,htmlAttributes: new {@clas="form-control" })
            @Html.ValidationMessageFor(model => model.TeamId, "", new { @class = "text-danger" })
        </div>
    </div>

在这段代码中,这是我得到的错误

  

没有类型为'IEnumerable'的ViewData项具有键'TeamId'。'

如果我将代码更改为此

@Html.DropDownList("TeamId",model=>model.TeamId,htmlAttributes: new {@clas="form-control" })

这是我收到的错误

  

无法将lambda表达式转换为“IEnumerable”类型,因为它不是委托类型

这也是我控制器中的Create方法

  #region [- Create() -]

    #region [- Get -]
    public ActionResult Create()
    {
        return View();
    }
    #endregion

    #region [- Post -]
    // POST: Member/Create
    [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult Create(string _fName, string _lName, string _describtion, string _pic, int _teamId, int _positionId)
    {
        try
        {
            // TODO: Add insert logic here
            Ref_Member = new View_Model.Buisiness.Member();
            //Ref_Member.CreateMember();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

这是我传递到我视图中的View模型

  #region [- Id -]
    [ScaffoldColumn(true)]
    [DisplayName("شماره")]
    public int Id { get; set; } 
    #endregion

    #region [- FName -]
    [DisplayName("نام")]
    [Required(ErrorMessageResourceType = typeof(Resources.Messages), ErrorMessageResourceName = "Required")]
    [StringLength(100, ErrorMessageResourceType = typeof(Resources.Messages), ErrorMessageResourceName  = "StringLength")]
    public string FirstName { get; set; }
    #endregion

    #region [- LName -]
    [DisplayName("نام خانوادگی ")]
    [Required(ErrorMessageResourceType = typeof(Resources.Messages), ErrorMessageResourceName = "Required")]
    [StringLength(100, ErrorMessageResourceType = typeof(Resources.Messages), ErrorMessageResourceName = "StringLength")]
    public string LastName { get; set; }
    #endregion

    #region [- Describtion -]
    [DataType(DataType.MultilineText)]
    [DisplayName("توضیحات")]
    public string Description { get; set; }
    #endregion

    #region [- Pic -]
    [DisplayName("عکس")]
    [Required(AllowEmptyStrings =false,ErrorMessageResourceType =typeof(Resources.Messages), ErrorMessageResourceName = "Required")]
    public string Pic { get; set; }
    #endregion

    #region [- TeamId -]
    [DisplayName("شماره تیم")]
    public Nullable<int> TeamId { get; set; }
    #endregion

    #region [- PositionId -]
    [DisplayName("شماره سرپرست")]
    public int PositionId { get; set; }

    #endregion

    #region [- Departments -]
    [DisplayName("دپارتمان")]
    public ICollection<Department> Departments { get; set; } 
    #endregion

    public  Position Position { get; set; }
    public  Team Team { get; set; }

    public  ICollection<Team> Teams { get; set; }
}

1 个答案:

答案 0 :(得分:0)

根据您收到的错误消息:@Html.DropDownList,您的IEnumerable调用似乎需要该参数中的集合,但您提供的是而是单一价值。

如果您查看documentation for the method overload you're calling,我认为您会发现它确认错误消息已经告诉您:它需要@Html.DropDownList("StudentGender", new SelectList(Enum.GetValues(typeof(Gender))), "Select Gender", new { @class = "form-control" }) ,但您提供的是单项。

This article提供了一些不错的代码示例。这是一个例子:

Enum.GetValues

注意使用@Html.DropDownListFor(m => m.StudentGender, new SelectList(Enum.GetValues(typeof(Gender))), "Select Gender") ,它产生一个IEnumerable。

要将其绑定到表单中的字段,您需要这样的内容:

<div class="my-table" data-bind="if:(dataList() && dataList().length)">
//render data raw here...

进一步阅读
Populating a razor dropdownlist from a List in MVC