In a C # MVC application can I send variables from a web page to controls that haven't changed?

I have a C # MVC application and <form> in my page.cshtml file. In this form, I have <input type="text" ... /> elements. If I submit this form, I only get the values ​​in Response.Params or Response.Form from the input, where I manually changed the value (i.e., Enter a text box and then typed something).

If I change the value using jQuery, $('#myInput').val('some value'); , this is not considered a change in the input value, and I do not get the value myInput when submitting the form.

Is there any way to conclude everyone ? If not, is there a good workaround, perhaps in some case that occurs before my model is attached? I need to know all the input values ​​from the form when they change, regardless of whether they have changed or not.

Additional Information:

The form and other values ​​get correct, and I get my model when the POST action is called in my controller.

The real problem is that my model is connected. It is created and bound to all values ​​except those that are not sent, because they are not in the Request.Params collection.

+4
source share
2 answers

I have ever seen this behavior when a field is disabled. Because of this, I usually have a javascript function that handles form submission and re-includes them in submit, so the correct values ​​are sent to the server.

Something like this does the trick for me (NOTE: I use jQuery):

 $(document).ready() { $("#ButtonSubmit").click(SubmitForm); } function SubmitForm(e) { e.preventDefault(); //ensure fields are enabled, this example does text and checkbox types $("[type='text']").attr("disabled", false); $("[type='checkbox']").attr("disabled", false); //submit the form document.forms[0].submit(); } 

I don’t know about an easier way to do this, it would be nice if you could “tag” something that instructs all the fields to send. But I do not know if this exists, maybe someone else can offer a better solution.

EDIT . It seems that disabled fields that are not sent are only the nature of HTML and are not something that is related to MVC.


It seems that if you create readonly fields instead of disabled , then the values ​​will still be sent. However, with this approach, you lose the "disabled" style. An exception to this rule is select control, it seems that this will not be sent to readonly . More information on this may be in this matter.

+3
source

Try using a razor helper to create a form tag.

@using (Html.BeginForm ()) {..

 // make sure this is a submit button <input type="submit" value="Save" /> 

}

In your post-action controller method, make sure you decorate it with [HttpPost]. eg.

[HttpPost] public ActionResult Edit (YourModel model) {

}

0
source

All Articles