I’m working on a asp.net mvc3 application. I have a @Html.DropDownList("abcd") and a @Html.CheckBox("efgh").
What I want:
If a defined option of the DropDownList will selected, the CheckBox should be disabled.
I think of sth. like that:
@if "defined option" is chosen of DropDownList("abcd") {
disable checkbox("efgh")
I’ve already found out how to disable a checkbox:
@Html.CheckBox("efgh", new { disabled = "disabled" })
As you can see, I’m using razor
Thanks in advance!! You guys doing a great job here 🙂
No, you should be thinking of a javascript function because the changes on the dropdown occur on the client, not on the server. So basically you need to sibscribe to the
onchangeevent of your dropdown, read the selected value and based on this value toggle the checkbox disability state.For example if you are using jQuery this might look something like this:
In my example
abcdis obviously the id of your dropdown andefghthe id of your checkbox. So make sure you they are correct and match your actual values.Oh and as a sidenote I don’t forget that if you disable an HTML input element, its value will never be sent to the server when the form is submitted. If you still want the value to be submitted to the server while the input cannot be modified you probably want to make it
readonlyinstead of disabling it.