Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding dynamic parameters with Html.BeginForm and jQuery submit

// html
<% using (Html.BeginForm("MyAction", "MyController", 
                   new { id = ViewContext.RouteData.Values["id"] },
                   FormMethod.Post, 
                   new { enctype = "multipart/form-data", class="myForm" }))
 { %>
    <input type="file" name="blah" />
 <% } %>



// script
$container.find('.myButton').click(function() {
    $container.find('.myForm').submit();
});

Before the form is submitted, I need to add some extra parameters (route values) which can only be calculated at the time of submit.

How do I do that?

like image 471
fearofawhackplanet Avatar asked Mar 17 '26 21:03

fearofawhackplanet


1 Answers

You could append a hidden field to the form before submitting it:

$container.find('.myButton').click(function() {
    var form = $container.find('.myForm');
    form.append(
        $(document.createElement('input'))
            .attr('type', 'hidden')
            .attr('name', 'somename')
            .attr('type', 'somecalculatedvalue')
    );
    form.submit();
});
like image 173
Darin Dimitrov Avatar answered Mar 20 '26 12:03

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!