1

I do have a MVC ASP.NET form that requires data from the user, specifically the user has nine [required] textboxes to fill with the due info and the most of the times the info of eight of those textboxes are the same so I let the user to fill all the fields of the form once and then press a duplicate button that allows the user to only specify the data of one textbox and the others eight textboxes will be filled with the data specified before...but the user is not allowed to change this duplicated info so I use jQuery

$("#idDireccion").attr("disabled", "disabled");
            $("#idEvento").attr("disabled", "disabled");
            $("#ddDepartamentos").attr("disabled", "disabled");
            $("#ddMunicipios").attr("disabled", "disabled");
            $("#idLugar").attr("disabled", "disabled");
            $("#datepicker").attr("disabled", "disabled");
            $("#idHoras").attr("disabled", "disabled");
            $("#idPrecio").attr("disabled", "disabled");  

and specify the duplicated value via ViewBag

@Html.EditorFor(model => model.lugarEvento, new { htmlAttributes = new { @class = "form-control left-border-none", @title = "especifique lugar en que impartirá el evento", @id = "idLugar", @Value = ViewBag.lugarEvento } })

I do this for the eight textboxes and it shows the info and does not let the user to change that info but when the user submits the form to the controller that handles the operations with the database doesn´t receive anything from the duplicated info(that is part of a ViewModel) all the values are null.

my question is what I have to do in order to not let the user to modify the duplicated info and still receive that duplicated info in my controller.

1 Answer 1

1

Don't use the disabled attribute use the readonly attribute instead. This will send the values to the controller.

        $("#idDireccion").attr('readonly', true);
        $("#idEvento").attr('readonly', true);
        $("#ddDepartamentos").attr('readonly', true);
        $("#ddMunicipios").attr('readonly', true);
        $("#idLugar").attr('readonly', true);
        $("#datepicker").attr('readonly', true);
        $("#idHoras").attr('readonly', true);
        $("#idPrecio").attr('readonly', true);  
Sign up to request clarification or add additional context in comments.

4 Comments

exactly, thanks...just one more question, how to specify readonly to a @Html.DropDownListFor ?
@PabloTobar that's a little trickier since drop downs don't have a readonly attribute. One way is to use a hidden field to store its value and pass that to the controller while disabling the dropdown.
I did use these, the first one is for a dropdown and the second one for jQuery datepicker, hope helps someone $('#ddDepartamentos option:not(:selected)').prop('disabled', true); $("#datepicker").datepicker({ minDate: -1, maxDate: -2 }).attr('readonly', 'readonly');
@PabloTobar another option will be to enable the field before the submit action happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.