0

I'm displaying a drop down menu using the following syntax:

@Html.DropDownListFor(model => model.Country, new SelectList(Model.CountryOptions, "Key", "Value"))

I have a Jquery statement which disabled the dd based on a check box:

$(document).ready(function () {
    $("#lockRecord").click(function () {
        if ($(this).attr('checked')) {
            $("#Country").attr('disabled', true);
        } else {
            $("#Country").attr("disabled", false);
        }
    })
});

However, when it's submitted, the value returns as null. Have tried readonly, but it doesn't work on drop down lists.

Any suggestions?

2
  • All answers lead to set hidden field via javascript, reference: stackoverflow.com/questions/2793736/… Commented May 16, 2012 at 16:51
  • Ask yourself this, if the dropdown is disabled and you are preventing the user from selecting a value, is Null a valid value for that property, such as causing a business rule to behave differently if not selected? If so then maybe you need to allow for Null Commented May 16, 2012 at 18:10

1 Answer 1

2

Take the value and submit it in a hidden field when the drop down is disabled. Of course, your server code will need to be modified to take that value, but that's a small price to pay.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a hidden input box which I set the correct value with: <input type="hidden" name=""CountryHidden"" id="CountryHidden" /> How do I ensure I catch it when it's submitted. I tried this on the controller, but it's not working: public ActionResult Edit(CustomerModel customer, string CountryHidden)
Why does your name attribute have double quotes?
If you add jQuery on the change event for the dropdown to update the value in the hidden input then your controller action can always use the value from the hidden input.

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.