如何在asp.net mvc中提交另一个表单内的表单

时间:2013-10-24 19:53:37

标签: jquery asp.net-mvc

我有一个带有提交按钮的Html表单,在表单中,我有多个部分需要单独提交。由于Html不支持嵌套表单,我该怎么做呢?并且每个部分都需要在提交之前进行验证。

<%  using (Html.BeginForm("actionName", "controllerName", FormMethod.Post))
        { %>
<form id="frmHomeContact" method="post"  >
                       -- a partial view is rendered here
                       <div class="grid-2-12">

                            <input style="width: 100%;" type="button" title="save" value="save" />
                        </div>
                      </form>

<%}%>

如果问题不明确,请告诉我。

2 个答案:

答案 0 :(得分:1)

$('#frmHomeContact').on('submit', function(){
    var $this = $(this); // catch a reference to the form
    $.ajax({
        url: '/', //this should probably be the path to your controller
        type: 'POST',
        data: $this.serialize(), //name=George&msg=hello..etc <--note $this not $(this)
        success: function(){
            console.log('Successful submission.');
        }
    });
    return false; //submitting the form will no longer cause the browser to refresh.
});

答案 1 :(得分:0)

您的HTML代码如下所示:

<%  using (Html.BeginForm("actionName", "controllerName", FormMethod.Post),new { id = "frmHomeContact" })
        { %>
<form  method="post"  >
                       -- a partial view is rendered here
                       <div class="grid-2-12">

                            <input style="width: 100%;" type="button" title="save" value="save" />
                        </div>
                      </form>
<%}%>
<script type="text/javascript">
    $("#frmHomeContact").submit(function() {
    var $this = $(this); // catch a reference to the form
        $.ajax({
            url: '/', //this should probably be the path to your controller
            type: 'POST',
            data: $this.serialize(), //name=George&msg=hello..etc <--note $this not $(this)
            success: function(){
                alert('Successful submission.');
            }
        });
        return false; //submitting the form will no longer cause the browser to refresh.
    });
</script>

您可以使用var txtname= $('#txtid').val();获取单个文本框的值,并检查txt名称是否为空。