How do I submit disabled input in ASP.NET MVC?
+3
A:
Typically, if I have a field that is "read-only" but needs to be submitted back to the server, I will make the display disabled (or simply text), but then add a hidden field with the same name. You still need to make sure that the field is not actually modified on the server-side -- just don't update it from the model in your action -- but the model state will still be accurate if there are errors.
tvanfosson
2010-04-23 17:42:18
Yes, I was thinking of doing what you're sugesting. But I would had to handle more variables.I'm hopping that may be my post will appear in google and people won't have to suffer, looking for an easy solution.
Sergio
2010-04-23 17:51:43
One problem with your solution is that it breaks if javascript is disabled. Injecting the hidden field server-side doesn't have this issue.
tvanfosson
2010-04-23 18:17:35
A:
Can't you make the field readonly="readonly"
instead of disabled="disabled"
? A readonly field value will be submitted to the server while still being non-editable by the user. A SELECT
tag is an exception though.
Darin Dimitrov
2010-04-23 20:50:55
A:
Thanks to everyone:
The way i resolved this:
document.getElementById("Costo").readOnly = true;
document.getElementById("Costo").style.color = "#c0c0c0";
Note:
I got this information on the answer but i got editted.
Sergio
2010-04-27 19:43:44