I suggest that you use the scaffolding function of Visual Studio to generate the controller and views to perform the CRUD operations and to see the code for the Edit. Described below is an example:
(1) Create the EDM from the Products, Categories and Suppliers tables of Northwind sample database:
(2) Perform the scaffolding to generate the controller and views for the CRUD operations for the Products table based on the above EDM.
(3) Execute the application and show the Edit view. Confirm that Categories and Suppliers are shown in drop downs and the items are selected properly:
(4) See the codes of Edit action method and the Edit.cshtml.
Edit action method
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Products products = await db.Products.FindAsync(id);
if (products == null)
{
return HttpNotFound();
}
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", products.CategoryID);
ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", products.SupplierID);
return View(products);
}
Edit.cshtml (drop down portions only)
<div class="form-group">
@Html.LabelFor(model => model.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("SupplierID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.SupplierID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CategoryID, "CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
</div>
</div>
(5) The DropDownList obtains the data given from ViewBag based on the name given in the 1st argument and selects the item according to the 4th argument of SelectList set in the Edit action method.
Note that the 2nd augment of @azzedinehtmlsql .DropDownList must be null. This is important to make the DropDownList to select the item according to the 4th argument of SelectList in the Edit action item.