How do I submit disabled input in ASP.NET MVC?
-
3Generally you need to phrase your question as a question and then post the answer separately. Voting to close; recommend you move the 'answer' part to an actual answer.George Stocker– George Stocker2010-04-23 18:24:06 +00:00Commented Apr 23, 2010 at 18:24
-
1I believe @Dhaval post is exactly answer because of disabled word in your question.QMaster– QMaster2016-09-11 14:46:43 +00:00Commented Sep 11, 2016 at 14:46
14 Answers
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.
4 Comments
<%: Html.TextBoxFor(model => model.p1, new { @readonly = "readonly" }) %> how can I gray it out so visually it looks like it is not editable. Better yet can we use something similar to LabelFor, I tried LabelFor but it only gets the DisplayName....type="checkbox" inputs eitherThanks 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.
4 Comments
textBox.Attributes.Add("readonly", "readonly"); and the explanation is in the link$("#textBoxId").css("color", "#c0c0c0").@ppumkin mentioned this on his comment on this answer but I wanted to highlight it as I was unable to find other resources on submitting data from a disabled <select>. I also believe it is relevant to the question as selects are not <input>s but they are "input"s.
Just include a Hidden field for the disabled select and its all sorted.
Example:
@Html.DropDownListFor(model => model.SelectedID, ... , new { disabled = "disabled"}) @* Won't be posted back *@
@Html.HiddenFor(model => model.SelectedID) @* Will be posted back *@
Caution: this will put two tags on the page with the same ID, which is not really valid HTML and could cause headaches with javascript. For me, I have javascript to set the value of the dropdown by its html id, and if you put the hidden field first, the javascript will find that instead of the select.
Comments
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.
2 Comments
You can also use code like this before the form submits:
$(":disabled", $('#frmMain')).removeAttr("disabled");
8 Comments
Ajax.BeginForm?By design browsers do not support that.
Either make them readonly which allows submitting values to server
or if you're dealing with controls that are still usable with readonly attribute such as Select, add css style pointer-events: none; to make them non-interactive
Kind of a hack, but works! It also works when you are submitting form directly with submit button without using javascript. No extra work required!
Eg:
<select asp-for="TypeId"
asp-items="@(new SelectList(await TypeRepository.FetchTypesAsync(), "TypeId", "Name"))"
class="form-control form-control-sm"
readonly
style="pointer-events: none;">
</select>
Comments
This will help in submit model values in ASP.net:
$("#iD").attr("style", "pointer-events: none;background-color:rgb(220,220,220)");
1 Comment
when we are dealing with disabled but checked checkboxes and we want to post the value, we need to ensure our hidden field appears before the @Html.CheckBoxFor hidden field.
following is the link from where I found the answer. http://davecallan.com/posting-disabled-checkboxes-mvc-razor-views/#comment-11033
Comments
I usually use this way for CheckBox or CheckBoxFor because making it disabled is causing the losing the value. Readonly doesn't work on checkbox neither.
@Html.DisplayFor(m => m.Order.Transfer)
@Html.HiddenFor(m => m.Order.Transfer)
1 Comment
expanding Tasos' (":disabled", $('#xxxForm')).removeAttr("disabled"); you can use:
$("#xxxForm").submit(function (e) {
e.preventDefault();
var form = this;
$('#Field1')[0].disabled = false;
$('#Field2')[0].disabled = false;
...
$('#FieldN')[0].disabled = false;
form.submit(); // submit bypassing the jQuery bound event
});
1 Comment
Just put this script in @section scripts in your page. this will enable inputs when you submit the page, and you should redirect user to another page after submit.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js">
$("form").submit(function () {
if ($('form').valid()) {
$("input").removeAttr("disabled");
}
});
</script>
Comments
Since the question is how to submit disabled input I am going to assume that you don't want to change the input to be readonly for what ever reason.
For example if your input is part of a button like a name attribute or value
<button class="myButton" type="submit" name="data[submit]">Submit</button>
and you are using that button to submit the form you may want to disable your button once you click it to submit your form. Disabled buttons won't have their data submitted in a form, so what can you do?
One way to ensure the button's data is included in the form submission is to capture the button's value and transfer it to a hidden input. This hidden input will be submitted with the form even when the button is disabled. Here's how you can achieve this using javascript:
var myForm = document.getElementById("myForm");
var myButton = document.querySelector('.myButton');
myButton.addEventListener("click", function () {
this.disabled = true;
var hiddenInput = document.createElement("input");
hiddenInput.type = "hidden";
hiddenInput.name = this.getAttribute('name');
myForm.appendChild(hiddenInput);
//Do other stuff...
myForm.submit();
});
Hope that helps.