This is a followup to this question: ASP.NET MVC Entity Framework CodeFirst Many To Many (CRUD)
I have exactly the same problem. (BTW: I am learning MVC after a lot of experience with ASP.NET WebForms)
(I need to ask a series of questions as I don't yet have 50 rep to add a comment.)
The accepted answer to that question says to add one line of code "inside your query code" to which the questioner said it worked.
My Create method is:
public ActionResult Create() {
db.Students.Include(s => s.Courses);
return View();
}
My Create View is currently what has been generated by the scaffolding engine when I created the Controller:
@model CodeTest.Models.Course
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Course</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Teacher, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Teacher)
@Html.ValidationMessageFor(model => model.Teacher)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
My first thought is: what else did the questioner do so it "just worked"?
This raises a series of questions: 1. Where is "inside the query code". Is it inside the create method of the controller? I tried that (see above) and it didn't work.
Did a dropdown box appear on the scaffolding screen by adding this one line of code? Was rescaffolding required? If so, how is that done?
Was anything else required to be added to the create and edit views to allow the user to set a value for this many to many relationship?