Home » c# » How do I submit disabled input in ASP.NET MVC?

How do I submit disabled input in ASP.NET MVC?

Posted by: admin November 29, 2017 Leave a comment

Questions:

How do I submit disabled input in ASP.NET MVC?

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.

Questions:
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

@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.

Questions:
Answers:

You can also use code like this before the form submits:

$(":disabled", $('#frmMain')).removeAttr("disabled");

Questions:
Answers:

You can create an editor template like the one below

CSS

.disabled {
    background-color:lightgray;
}

Editor Template

@model string
@Html.TextBoxFor(x => x,new {@class="disabled", @readonly=true })

Questions:
Answers:

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