Good morning, I'm learning c # ASP .NET MVC,
How can you convert a table to IEnumerable
to use it in Dropdownlistfor
?:
I have the following model:
public class Saleform
{
public int ID { get; set; }
public int SaleaccountID { get; set; }
public int LeadsourceID { get; set; }
public int SalestageID { get; set; }
public int VerifyCodeViewModelCode { get; set; }
public string Name { get; set; }
public float Likely { get; set; }
public DateTime Dateup { get; set; }
public virtual Salestage Salestage { get; set; }
public virtual Leadsource Leadsource { get; set; }
public virtual Saleaccount Saleaccount { get; set; }
}
In another file I have the model referenced:
public class Leadsource
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Saleform> Forms { get; set; }
}
In the controller:
public ActionResult Create()
{
IEnumerable<Leadsource> Ileadsourcelist = (from x in db.Leadsources
select x).ToList<Leadsource>();
ViewData["Leadsourcenamelist"] = Ileadsourcelist;
return View();
}
In the view:
@Html.DropDownListFor(model => model.LeadsourceID,
(IEnumerable<SelectListItem>)ViewData["Leadsourcenamelist"])
The error that marks is that it can not convert information of type List
to type IEnumerable
.
Thank you.