0

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

            });
        });
2
  • Is dropDown1 the id of your dropdown? In that case change your jquery to $("#dropDown1").attr("disabled", true) Commented Aug 1, 2016 at 15:46
  • Yes I did like that but still nothing it was mistake when I wrote this question thx anyway. Commented Aug 1, 2016 at 15:47

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

14 Comments

But how can I set previous value I can set it in sessionStorage ?
Where do you get the previous value from ? If it is from your db table, you may query it again in your http post action. If it is from the previous form, you might keep that in a hidden field in the form.
Yes I want to change information about selected value from my table I never do this I try to disabled but doesn't work as you said
You still did not say where you get the previous value from ? Also, you want to simply show just the previous selection to the user ? Please share your code how you are rendering the dropdown.
I get the value from the database I have table in my view and edit/delete/details options when I click on edit I got that informations on my Edit view
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.