收录日期:2019/12/11 02:49:14 时间:2013-08-22 12:46:18 标签:asp.net-mvc

Can posted input values on onbegin of ajax.beginform be modified? I have to modify values of some of the input fields after the form is submitted. But even if I change the values through js, at server side in request.form I am getting the old values which were set initially at the time of form submit. How to get the modified values in request.form?

The code block is as follows:

<% using(Ajax.BeginForm("action", "controller",
     new AjaxOptions{onbegin="funBegin",oncomplete="funComplete"})){ 
%>

<input type="text" id="txtName" name="txtName" value="gaurav"/>
<input type="text" name="txtAge" value="26"/>
<input type="submit" value="click here" />

<% } %>

<script type="text/javascript">
    function funBegin() {
        $("#txtName").val("gaurav pandey");
    }
    function funBegin(result) {
        $("#divParent").html(result.get_data());
    }
</script>

Now when I try to get request.form["txtname"] at server side, I am getting "gaurav" instead of "gaurav pandey".

You're having this issue because funBegin is called after the form data has been serialized. From MSDN:

AjaxOptions.OnBegin Property: Gets or sets the name of the JavaScript function to call immediately before the page is updated.

I suggest you write your own submit handler:

<form id="myform" action="/Home/MyAction">
    <input type="text" id="txtName" name="txtName" value="gaurav" />
    <input type="submit" value="Submit" />
</form>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#myform').submit(function() {
            $("#txtName").val("gaurav pandey");

            var form = $(this);
            var url = form.attr('action');
            var formData = form.serialize();
            $.post(url, formData, function(result) {
                // Do something with result
            });

            return false;
        });
    });
</script>

I didn't feel like going outside of MVC's form submission handling so I solved this differently. Just bind to the form's submit button's click handler. Do your input value fiddling there. The onclick is fired before MVC's form serialization. The onclick also gets called even if the form is submitted by the user hitting enter.

There is another way to do that :

function funBegin(jqXHR, settings) {
    // before POST - change whatever posted value you want and then 
  // serialize again your form supposedly identified here by form

        settings.data = form.serialize();

  // the advantage is when you arrive here, the form validators have already been executed
  // and you know you have valid values - so you can post
    }
}

For meaning of parameters, see [http://api.jquery.com/jquery.ajax/]

$('#myform').submit() won't work on IE. If there is any validation, you can try to create a fake validation method to collecting form data.

like Jquery.validaton below:

jQuery.validator.addMethod("fakeMethod", function (value, element) {
                YOUR DOM CODE HERE!!!
                return true;
            }, "");