jhony3
jhony3

Reputation: 272

How to disable the selection of DropDownList ASP.NET MVC?

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);
                }

            });
        });

Upvotes: 0

Views: 582

Answers (1)

Shyju
Shyju

Reputation: 218702

This is the expected behavior. If you disable a form element, when you submit the form, the value will not be submitted.

If you are disabling the field, that means you do not want the user to input the value for this field, in that case, you should set the value in your HttpPost action method.

Never simply rely on client side. Always do server side validations. Remember, even if you disable a form element, user can re-enable it using his browser dev tools and submit the form.

EDIT : As per your comments, you do not want to update one dropdown value in your table when updating a record. In that case, you may simply exclude that property from your Bind in your HttpPost action.

Assuming you do not want to update the existing value of InspekcijskoTijeloId property/column, just exclude that from the Bind.

[HttpPost]   
public ActionResult Edit([Bind(Include = "InspekcijskaKontrolaId,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);
}

Upvotes: 1

Related Questions