I want to prevent DropDownList from selecting new value but I want to keep previous selected value for example when I want to change information of some value from database I tried like this $("#dropDown1").attr("disabled","disabled") but this doesn't keep the previous value and my application is broken every time when I try on this way any suggestion ?
This is my controller method:
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
IQueryable<InspekcijskaKontrola> inspekcijskeKontrole = db.InspekcijskeKontrole.Include(i => i.InspekcijskaTijela).Include(i => i.Proizvod).Select(i => i);
InspekcijskaKontrola inspekcijskaKontrola = inspekcijskeKontrole.Where(i => i.InspekcijskaKontrolaId == id).Select(i => i).Single();
if (inspekcijskaKontrola == null)
{
return HttpNotFound();
}
ViewBag.InspekcijskoTijeloId = new SelectList(db.InspekcijskaTijela, "InspekcijskoTijeloId", "NazivInspekcijskogTijela", inspekcijskaKontrola.InspekcijskoTijeloId);
ViewBag.ProizvodId = new SelectList(db.Proizvodi, "ProizvodId", "NazivProizvoda", inspekcijskaKontrola.ProizvodId);
return View(inspekcijskaKontrola);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "InspekcijskaKontrolaId,InspekcijskoTijeloId,ProizvodId,DatumInspekcijskeKontrole,Rezultat,ProizvodSiguran")] InspekcijskaKontrola inspekcijskaKontrola)
{
if (ModelState.IsValid)
{
db.Entry(inspekcijskaKontrola).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.InspekcijskoTijeloId = new SelectList(db.InspekcijskaTijela, "InspekcijskoTijeloId", "NazivInspekcijskogTijela", inspekcijskaKontrola.InspekcijskoTijeloId);
ViewBag.ProizvodId = new SelectList(db.Proizvodi, "ProizvodId", "NazivProizvoda", inspekcijskaKontrola.ProizvodId);
return View(inspekcijskaKontrola);
}
This is how I tried to change informations with $.ajax maybe I should use PUT ?
$("#btnSave5").click(function (e) {
e.preventDefault();
var form = $(this).closest("form");
$.ajax({
type: "POST",
url: form.attr("Edit"),
data: form.serialize(),
success: function (response) {
alert("Informations are successfully changed");
window.location.href = "/InspekcijskeKontrole/Index";
},
error: function (error) {
alert(error);
}
});
});
dropDown1the id of your dropdown? In that case change your jquery to$("#dropDown1").attr("disabled", true)