Adding dynamic parameters with Html.BeginForm and jQuery submit

只谈情不闲聊 提交于 2019-12-18 18:09:08

问题


// 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:


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();
});


来源:https://stackoverflow.com/questions/3761051/adding-dynamic-parameters-with-html-beginform-and-jquery-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!