2

In my view i have a problem and my dropdownlist fails and doesn't work don't know why

@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>

in this code this is the error that i get

There is no ViewData item of type 'IEnumerable' that has the key 'TeamId'.'

if i change the code to this one

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

this is the error that i receive

Cannot convert lambda expression to type 'IEnumerable' because it is not a delegate type

also this is my Create method in my controller

  #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();
        }
    }

and this is my View model that i passed into my 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; }
}
user9306199
  • 83
  • 1
  • 2
  • 9
  • Is it really supposed to be `@clas=`, and not `@class = `? – Robert Harvey Feb 02 '18 at 19:30
  • @RobertHarvey i fixed it but still i get the same error – user9306199 Feb 02 '18 at 19:33
  • Well, based on the error message you're receiving: `Cannot convert lambda expression to type 'IEnumerable' because it is not a delegate type`, it appears that your `@Html.DropDownList` call expects a *collection* in that parameter, but you are providing a single value instead. – Robert Harvey Feb 02 '18 at 19:36
  • And if you look at the [documentation for the method overload you're calling](https://msdn.microsoft.com/en-us/library/gg537935(v=vs.111).aspx), I think you'll find that it confirms what the error message is already telling you: it expects an `IEnumerable`, but you're providing it with a single item. – Robert Harvey Feb 02 '18 at 19:40
  • @RobertHarvey but it's a form it must be simple and single not a Collection – user9306199 Feb 02 '18 at 19:48
  • You need a list of things in your dropdown, not a single item. If you only have a single item to choose from, then you don't need a dropdown. – Robert Harvey Feb 02 '18 at 19:49
  • how can i show a collection of stuff in `dropdownlist` – user9306199 Feb 02 '18 at 19:52
  • Have a look at this example: https://stackoverflow.com/a/18382610 – Robert Harvey Feb 02 '18 at 20:07

1 Answers1

0

Well, based on the error message you're receiving: Cannot convert lambda expression to type 'IEnumerable' because it is not a delegate type, it appears that your @Html.DropDownList call expects a collection in that parameter, but you are providing a single value instead.

And if you look at the documentation for the method overload you're calling, I think you'll find that it confirms what the error message is already telling you: it expects an IEnumerable, but you're providing it with a single item.

This article provides some decent code samples. Here is an example:

@Html.DropDownList("StudentGender", 
                    new SelectList(Enum.GetValues(typeof(Gender))),
                    "Select Gender",
                    new { @class = "form-control" })

Notice the use of Enum.GetValues, which produces an IEnumerable.

To bind it to a field in your form, you need something like this:

@Html.DropDownListFor(m => m.StudentGender, 
                    new SelectList(Enum.GetValues(typeof(Gender))), 
                    "Select Gender")

Further Reading
Populating a razor dropdownlist from a List in MVC

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501