This is a really elementary question and I have searched for an answer but there seems to be none to be found. This is dealing with the Scaffolded MVC5 edit.cshtml using VS2015.
I have a 1:M relationship between 2 tables, and the child table has a @Html.DropDownList as a foreign key to the parent. I need to filter that @Html.DropDownList. These are the specifics:
One AssociationList has many AgsweepParameters.
The primary key of AssociationList is AssociationListId Therefore the AssociationListId field is the Foreign key in the AgsweepParameter table.
Using EntityFramework, and MVC5 Scaffolding, I created the crud views/controllers using Entity Framework as the model.
The generated declaration for that dropdown looks like this:
<div class="form-group">
@Html.LabelFor(model => model.AssociationListId, "Association:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AssociationListId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AssociationListId, "", new { @class = "text-danger" })
</div>
</div>
Question: How do I filter that dropdown list so that AssociationList.AssociationType=="ACA"?
Note that I use a similar filter on the Index.cshtml on AssociationList table which looks like this:
@foreach (var item in Model.Where(row=>row.AssociationType=="ACA")) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.AssociationListId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.AssociationListId })
</td>
</tr>
}
Fundamental MVC here. I had to do the following:
Understand that the controller is the object that defines the filter and not the view. Therefore I go to the controller for public ActionResult Edit(int? id) and change
ViewBag.AssociationListId = new SelectList(filteredAssociations, "AssociationListId", "Name", agsweepParameter.AssociationListId);
to
IEnumerable<AssociationList> filteredAssociations = db.AssociationLists.Where(row => row.AssociationType == "ACA");
ViewBag.AssociationListId = new SelectList(filteredAssociations, "AssociationListId", "Name", agsweepParameter.AssociationListId);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.