views:

53

answers:

1
// 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?

+1  A: 

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();
});
Darin Dimitrov
I agree with your answer, but since this is a post-back scenario (presumably with model binding), I would suggest that the hidden input field should already be stubbed out (i.e. not generated on the fly) -- it should merely have its value changed.
Kirk Woll
@Kirk, yes that's a good idea. The input could be already be generated on the server side and only set it's value on the client unless the number of those hidden fields is dynamic and added by the user.
Darin Dimitrov
i can't really have static hidden fields, as I have an unlimited number of forms on the page and every submit needs this extra information as it is for error recovery. I can have a single hidden field in the master page, but then again it's the problem that i don't know how to get this value to submit with the form.
fearofawhackplanet