In my ASP.NET MVC application I'm using viewbag to bind the combo box on the create view.
public ActionResult Create()
{
ViewBag.Country_Id = new SelectList(db.Countries, "Id", "Country_Name");
return View();
}
View
@Html.DropDownList("Country_Id", null, htmlAttributes: new { @class = "form-control" })
Now in the Edit view, I pass the same to the view. But the combo box not showing the text name related to the model ID. How to assign it to the dropdownlist in the edit view
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ViewBag.Country_Id = new SelectList(db.Countries, "Id", "Country_Name");
var provinces = db.Province.FirstOrDefault(u => u.Id == id);
if (provinces == null)
{
return HttpNotFound();
}
return View(provinces);
}
Edit View
@Html.DropDownList("Country_Id", null, htmlAttributes: new { @class = "form-control" })