4

I have a C# MVC application and a <form> in my page.cshtml file. In that 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 inputs where I changed the value manually (i.e. Entered the text box then typed something).

If I change the value with jQuery, $('#myInput').val('some value'); this does not count as a change in the input's value and I do not get myInput's value when I submit the form.

Is there any way to make sure all inputs are submitted? If not then is there a good workaround for this, maybe in some event that occurs before my model gets bound? I need to know all the input values from the form when submitted whether they changed or not.

Some additional info:

The form and other values are getting submitted correctly and I am receiving my model when the POST action is called in my controller.

The real issue is when my model is being bound. It is being created and bound with all values except the one not being submitted because it is not in the Request.Params collection.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Mike Webb
  • 8,855
  • 18
  • 78
  • 111
  • 3
    That should not happen. Show us your HTML. – SLaks Apr 29 '13 at 15:15
  • Just accept the model as a parameter on your Action and you can use as-is. – Belogix Apr 29 '13 at 15:15
  • Strange, I have never seen this. It always posts value regardless of how the were changed, or if they even changed at all – musefan Apr 29 '13 at 15:15
  • Have you looked in Fiddler to see how the values are being submitted? – mgnoonan Apr 29 '13 at 15:16
  • Is your controller action receiving a model object or a more generic FormCollection? – theyetiman Apr 29 '13 at 15:16
  • 1
    Actually I seem to remember disabling fields caused them not to POST, are you doing this? – musefan Apr 29 '13 at 15:18
  • @musefan, that's it! Never would have put the two together. I am disabling the field and I need to have it disabled. Can I specify somewhere that disabled fields should still be submitted or do I have to enable it right before I submit the form? – Mike Webb Apr 29 '13 at 15:24
  • @MikeWebb: I have added an answer which explains a technique that I have used to solve this problem – musefan Apr 29 '13 at 15:28

2 Answers2

3

I have only ever seen this behaviour when a field is disabled. Due to this, I commonly have a javascript function that handles the form submission and re-enables them on submit, this way the correct values get sent to the server.

Something like this does the trick for me (NOTE: I am using 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 am unaware of any easier way to do this, it would be nice if you could 'flag' something that instructs all fields to be submitted. But I don't know if this exists, maybe somebody else can offer a better solution.

EDIT: It appears that disabled fields not submitting is just the nature of HTML, and is not something that is tied to MVC.


It seems that if you make the fields readonly instead of disabled then the values will still submit. However, with this approach you lose the 'disabled' styling. The exception to this rule is select control, it seems this will not submit under readonly either. More information on this can be in this question

Community
  • 1
  • 1
musefan
  • 47,875
  • 21
  • 135
  • 185
0

Try using the razor helper to build the form tag.

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

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

}

In your controller action post method make sure you decorate it [HttpPost]. e.g.,

[HttpPost] public ActionResult Edit(YourModel model) {

}

Tib
  • 272
  • 2
  • 5
  • This doesn't solve the issue, this just shows how to submit a form. Which the OP seems to already be able to do. Also, please format your code correctly – musefan Apr 29 '13 at 15:36
  • @Tib, the issue is actually not at the Controller/Action level. The issue is that the value is not in the `Request.Params` collection which is sent to the server and used to build the model that is passed to the Action. The resulting Model in my case is incomplete because I am not receiving all the input values from the form submit. – Mike Webb Apr 29 '13 at 15:37