I am trying to populate & bind a drop down list control in MVC using Dapper, and I am having trouble finding a good example online. I can find plenty of examples of how to bind a statically defined drop down to a update a database (Like "Male" or "Female") and can get that to work. I can also find many examples of populating a drop down from a database, but I cannot get them to work in conjunction.
I just want my ProjectManifest/Create form to have a drop down populated with my current Projects.
I wish I was better at asking this question, but I do not know what I am doing wrong. I have tried dozens of variations in my code and different tutorials and questions on StackOverflow. The errors I receive from Visual Studio are so unhelpful, they are usually just NullReference exceptions, and there are so many variables in the HTML Helper function call I do not even know what is null.
These are some of the articles I have tried to use to help understand my problem:
- Populate a Dropdown List in MVC View
- User HTML Helper to populate a DropDown list
- Value cannot be null. Parameter name: entitySet
- https://www.youtube.com/watch?v=k9CJmLbnFko
- http://dotnetmentors.com/mvc/how-to-bind-dropdownlist-in-asp-net-mvc-application.aspx
- Binding Data To DropDownList MVC Razor
- Asp.Net MVC with Drop Down List, and SelectListItem Assistance
I think the problem is in my index file, I do not understand Lambda expressions or what the expectation is to be in the arguments for the Html helper "DropDownListFor" or "DropDownList"
Index view
@using (Html.BeginForm("Index", "ProjectManifests", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
....
@Html.LabelFor(model => model.ManifestID)
@Html.EditorFor(model => model.ManifestID)
@Html.ValidationMessageFor(model => model.ManifestID)
@Html.DropDownListFor(model => model.ProjectID, Model.Projects)
....
<input type="submit" value="Create" class="btn btn-default" />
}
Controller
public class ProjectManifestsController : Controller
{
[HttpGet]
public ActionResult Index()
{
Models.ProjectManifestModel model = new Models.ProjectManifestModel();
DatabaseHelper.ConnectionString = ConfigurationManager.ConnectionStrings["MTConnectionString"].ConnectionString;
ProjectRepository pr = new ProjectRepository();
List<SelectListItem> projects = new List<SelectListItem>();
foreach (Project p in pr.GetProjects())
{
projects.Add(new SelectListItem { Value = p.ProjectID.ToString(), Text = p.ProjectName });
}
model.Projects = projects;
ViewBag.Projects = projects;
return View();
}
}
ViewModel
public class ProjectManifestModel
{
[Key]
public int ManifestID { get; set; }
public int ProjectID { get; set; }
....
public HttpPostedFileBase Attachment { get; set; }
public IEnumerable<SelectListItem> Projects { get; set; }
}
The state of this code produces a different error, my apologies:
Compiler Error Message: CS1503: Argument 3: cannot convert from 'System.Collections.Generic.IEnumerable<Poscarp.TabLibrary.Project>' to 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>'
Source Error:
Line 29: @Html.DropDownListFor(model => model.ProjectID, Model.Projects)