Adding dynamic parameters with Html.BeginForm and

2019-02-11 05:32发布

// 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条回答
【Aperson】
2楼-- · 2019-02-11 06:19

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();
});
查看更多
登录 后发表回答