1

I develop a web application below ASP.NET MVC 4 with Razor Views.

I noticed now that SomethingID is empty in the ViewModel after Postback (to the Controller)
if I only add the disabled HTML attribute to DropDownListFor:

// SomethingID becomes 0:
@Html.DropDownListFor(m => m.SomethingID, Model.SomethingList, new { @disabled = "disabled" })

It doesn't happen if I remove the disabled Attribute, SomethingID has the proper value then:

// SomethingID keeps its value (greater than 0):
@Html.DropDownListFor(m => m.SomethingID, Model.SomethingList)

Has someone experienced that problem already? At the moment, I try to solve the problem with jQuery, by storing the value in a HiddenFor field.

But my question would be: Is there a better solution? Is it intended behaviour from Microsoft that Dropdownlists shall forget everything as soon as they are disabled? Do I do a mistake? Is it a bug?

Remark: I have 5 Dropdownlists in my View, all 5 show the same behaviour.

Many thanks in advance!!!

Santiago Hernández
  • 5,438
  • 2
  • 26
  • 34
Jana Weschenfelder
  • 840
  • 2
  • 8
  • 23
  • 1
    Setting the input to disabled will prevent it from posting. You can use `Readonly` rather than disabled. See this question: http://stackoverflow.com/questions/1355728/values-of-disabled-inputs-will-not-be-submited – disappointed in SO leadership Jul 27 '15 at 15:34
  • Many thanks to you for the hint!!! I will try it. – Jana Weschenfelder Jul 27 '15 at 15:38
  • 1
    Great!!! That solves my problem!!! Thanks a lot!!! :-) – Jana Weschenfelder Jul 27 '15 at 15:44
  • Hi, I have to rework my last answer here. The readonly seemed to work first. But the debugging tool then told me that I still could select a value from the dropdownlist although it was readonly and that the new selected value was sent to the Controller then. I went the following way now: SomethingID is stored in a hidden field, and the text of Something is displayed in a readonly textbox now. I should have used textboxes in that special case earlier. LOL. I learned in that way... – Jana Weschenfelder Jul 29 '15 at 12:50

1 Answers1

4

Quite simply the reason you are seeing this is because elements that have the disabled attribute do not have their values posted to the server. See this SO post for more detail.

Community
  • 1
  • 1
Ric
  • 12,855
  • 3
  • 30
  • 36
  • Many thanks for the fast answer! I have to wait some minutes before I can accept your answer. – Jana Weschenfelder Jul 27 '15 at 15:36
  • You're welcome. You basically discovered this on your own by removed the `disabled` attribute and noticing the value was posted. – Ric Jul 27 '15 at 15:36