I am trying to make Edit view work with DropDownListFor in ASP MVC 5, but whatever I select, the posted value remains the old one. I find that odd because, initially, selected item is the actual model value. And that goes for both DropDownListFor elements I have on the page.
Furthermore, the same logic works on the Create view of the same controller.
Model properties:
public List<ProizvodjacViewModel> VendorList { get; set; }
public List<KategorijaViewModel> CategoryList { get; set; }
View elements:
<div class="form-group">
@Html.LabelFor(model => model.ProizvodjacID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.ProizvodjacID, new SelectList(Model.VendorList, "ID", "Naziv"))
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.KategorijaId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.KategorijaId, new SelectList(Model.CategoryList, "ID", "Naziv"))
</div>
</div>
The controller is not important since the values posted to it are already bad.
What I did discover is that in the Create view, HTML that is generated is just a plain list:
<select data-val="true" data-val-number="The field ProizvodjacID must be a number." data-val-required="The ProizvodjacID field is required." id="ProizvodjacID" name="ProizvodjacID" aria-required="true" aria-describedby="ProizvodjacID-error" class="valid" aria-invalid="false">
<option value="1">Gibson</option>
<option value="2">Hohner</option>
<option value="3">Yamaha</option>
</select>
But in the edit view, a selected
attribute is also generated:
<select id="ProizvodjacID" name="ProizvodjacID" aria-required="true" aria-describedby="ProizvodjacID-error" class="valid" aria-invalid="false">
<option selected="selected" value="1">Gibson</option>
<option value="2">Hohner</option>
<option value="3">Yamaha</option>
</select>
Controller methods:
// GET: Proizvod/Edit/5
[HttpGet]
[ActionName("Edit")]
public ActionResult EditGet(int id)
{
ViewBag.Err = "";
ProizvodViewModel p = new ProizvodViewModel()
{
ID = id,
Naziv = "",
NazivProizvodjaca = "",
CenaOd = 0,
CenaDo = Int32.MaxValue
};
//goes to DB and gets a product(Proizvod) by id
DataSet ds = DAL.ProizvodDAL.Pretraga(p);
DataRow dr = ds.Tables[0].Rows[0];
p.Naziv = dr["NAZIV"].ToString();
p.Cena = Convert.ToDecimal(dr["CENA"]);
p.SlikaUrl = dr["SLIKA_URL"].ToString();
p.ProizvodjacID = Convert.ToInt32(dr["PROIZVODJAC_ID"]);
p.NazivProizvodjaca = dr["NAZIV_PROIZVODJACA"].ToString();
p.KategorijaId = Convert.ToInt32(dr["KATEGORIJA_ID"]);
//populates VendorList from DB -- code below
popuniProizvodjace(p);
//same, but with CategoryList -- code below
popuniKategorije(p);
return View(p);
}
// POST: Proizvod/Edit/5
[HttpPost]
[ActionName("Edit")]
public ActionResult EditPost(ProizvodViewModel proizvod)
{
try
{
// TODO: Add update logic here
if (ModelState.IsValid)
{
if (proizvod.ListaKategorija == null)
popuniKategorije(proizvod);
if (proizvod.ListaProizvodjaca == null)
popuniProizvodjace(proizvod);
int rezultat = 1;
string poruka;
//updates the product in DB
DAL.ProizvodDAL.Izmena(proizvod, out rezultat, out poruka);
if (rezultat != 0)
{
ViewBag.Err = poruka;
return View(proizvod);
}
return RedirectToAction("Index");
}
else
{
return View(proizvod);
}
}
catch (Exception ex)
{
ViewBag.Err = ex.Message;
return View(proizvod);
}
}
private void popuniProizvodjace(ProizvodViewModel proizvod)
{
// gets all vendors (Proizvodjac) from DB
DataSet ds = DAL.ProizvodjacDAL.Pretraga(new ProizvodjacViewModel() { Naziv = "", ID = 0 });
if (AetFunkcije.proveriDataSet(ds))
{
proizvod.VendorList = new List<ProizvodjacViewModel>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
proizvod.VendorList.Add(new ProizvodjacViewModel() { ID = Convert.ToInt32(dr["PROIZVODJAC_ID"]), Naziv = dr["NAZIV"].ToString() });
}
}
}
private void popuniKategorije(ProizvodViewModel proizvod)
{
// gets all categories (Kategorija) from DB
DataSet ds = DAL.KategorijaDAL.Pretraga(new KategorijaViewModel() { ID = 0, Naziv = "" });
if (AetFunkcije.proveriDataSet(ds))
{
proizvod.ListaKategorija = new List<KategorijaViewModel>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
proizvod.CategoryList.Add(new KategorijaViewModel() { ID = Convert.ToInt32(dr["KATEGORIJA_ID"]), Naziv = dr["NAZIV"].ToString() });
}
}
}
Any advice?
Try following code:
Model
ProizvodjacViewModel
Controller
View