ASP MVC:使用AJAX.ActionLink时提交按钮不起作用

时间:2016-02-10 19:00:57

标签: asp.net ajax asp.net-mvc twitter-bootstrap

你可以帮忙解决这个问题:

使用AJAX.ActionLink尝试打开引导模式后,提交按钮在简单的ASP MVC页面中不起作用,但是当我尝试直接打开(没有AJAX.ActionLink)它正在工作然后调用控制器&#39 ;服务器端的操作:这是代码 主页:index.cshtml

@model IEnumerable<TestQuestionsAnswers.Models.Question>

@{
    ViewBag.Title = "Index";
}
<script>
    function ShowPopup() {

        $("#divmodal").attr('data-toggle', 'modal');
        $('#divmodal').modal('show');
    }
</script>

<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
    @Ajax.ActionLink("Validate Data!", "Edit1", "Question", new { id = 1 }, new AjaxOptions { UpdateTargetId = "divresponse", OnComplete = "ShowPopup" }, new { @class = "btn btn-info btn-sm" })
</p>

<!-- Modal -->
<div id="divmodal" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Question</h4>
            </div>
            <div class="modal-body">

                <div class="container" id="divresponse">Data is here....</div>
                <input type="submit" class="btn btn-info" name="btnsubmit" value="Save">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

请注意:从上面的代码中我使用:

@Ajax.ActionLink("Validate Data!", "Edit1", "Question", new { id = 1 }, new AjaxOptions { UpdateTargetId = "divresponse", OnComplete = "ShowPopup

打开模态窗口。

Edit.chtml:

@model TestQuestionsAnswers.Models.QuestionEditView

@{
    ViewBag.Title = "Edit";
}


@using (Html.BeginForm("Save", "Question", FormMethod.Post))
{

    <input type="submit" class="btn btn-info"   name="btnsubmit" value="Save" >





}

和控制者应该达到的行动是:

   [HttpPost]
        public ActionResult Save(string btnsubmit, TestQuestionsAnswers.Models.QuestionEditView questedit)
        {
            if (btnsubmit == "Save")
            {
                List<Answer> UpdatedAnswers=questedit.Answers;

                dbaccess.SaveEditQuestionsBy(questedit);
            }

            return RedirectToAction("Index");
        }

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我不知道你是否安装了unobtrusive-ajax包 但如果不是先去管理nudget包 - &gt;搜索ajax并安装Microsoft.jquery.unobtrusive-ajax.js

然后在你的cshtml中提供这个引用

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

然后将调试器放入控制器并检查方法是否正在触发。