我的模态弹出窗口内的.submit(function())不起作用,我的模态弹出窗口将发送正常的http post请求

时间:2015-01-20 02:45:40

标签: javascript jquery ajax asp.net-mvc razor

我正在开发一个asp.net mvc Web应用程序。在我的主视图中,我得到以下创建链接: -

    <a class="btn btn-success" data-modal="" href="/Staff/Create" id="btnCreate">
    <span class="glyphicon glyphicon-plus"></span>      
    </a>


<!-- modal placeholder-->
<div id='myModal' class='modal fade in'>
    <div class="modal-dialog">
        <div class="modal-content">
            <div id='myModalContent'></div>
        </div>
    </div>
</div>

我有以下脚本: -

$(function () {
    $.ajaxSetup({ cache: false });
    $("a[data-modal]").on("click", function (e) {

        $('#myModalContent').load(this.href, function () {
            $('#myModal').modal({
                keyboard: true
            }, 'show');
            $('#myModalContent').removeData("validator");
            $('#myModalContent').removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse('#myModalContent');
            bindForm(this);
        });
        return false;
    });


});

function bindForm(dialog) {
    $('#myModalContent', dialog).submit(function () {

        if ($('#myModalContent').valid()) {
            $('#progress').show();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    if (result.success) {
                        $('#myModal').modal('hide');
                        $('#progress').hide();
                        //location.reload();
                        alert('www');
                    } else {

                        $('#progress').hide();
                        $('#myModalContent').html(result);
                        bindForm();
                    }
                }
            });
     }
        else {
           return false;
   }
    });
}

现在,当我点击Create链接时,Create action方法将返回以下部分视图,该视图将在模态弹出窗口中呈现: -

@model SkillManagement.Models.Staff

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Staff</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.GUID, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GUID)
                @Html.ValidationMessageFor(model => model.GUID)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.UserName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserName)
                @Html.ValidationMessageFor(model => model.UserName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IsExternal, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.IsExternal)
                @Html.ValidationMessageFor(model => model.IsExternal)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
        </div>

        //code goes here
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

到目前为止我已经完成了所有工作,将调用Get Create动作方法,并在模态弹出窗口中呈现局部视图。

但现在在我的局部视图中,如果我点击&#34;创建&#34;按钮,将调用Post创建操作方法,但不是由于javascript代码。当我在我的Post创建动作方法中检查Request.IsAjax()时,我得到它不是ajax请求,这意味着部分视图发送正常的Post http而不是脚本中定义的ajax请求,任何人都可以建议什么我目前的做法有误吗? 感谢

2 个答案:

答案 0 :(得分:1)

你可以看到你只是将#myModalContent节点传递给bindForm函数,jQuery选择器查找

// will never find #myModalContent
$('#myModalContent', myModalContentDOMElement).submit(function () {

相反,你应该做这样的事情

$('form', dialog).submit(function (e) {
    e.preventDefault(); // stop the default form submit action

答案 1 :(得分:0)

您正在通过ajax将表单加载到页面中,但是如果您希望表单本身使用ajax,则您正在加载的表单是常规html表单,我相信正在寻找@Ajax.BeginForm()

msdn documentation

@using (Ajax.BeginForm({objectparams})){
...