Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

asp.net mvc - Modify posted input values on onbegin of ajax.beginform

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...