asp.net mvc ajax.beginform作为html.beginform发送

唐吉jo德

我有一个局部视图,希望从该视图显示包含更新数据的模式对话框。用户单击div将同时触发模态的显示和ajax调用,以更新模态的内容。

<div class="nMmenuItem" >    
    @using (Ajax.BeginForm("editItem","nMrestaurant",new { id = Model.ID },
        new AjaxOptions
        {
            HttpMethod = "get",
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId = "myModalDocument"
        }, new { id = "ajaxEditItem" }))
    {
        <div data-toggle="modal" data-target="#myModal" 
             onclick="$('form#ajaxEditItem').submit();">            
            <div class="text-center">
                @Model.name
            </div>
        </div>        
    }
</div>

我在父视图中有一个模态的占位符:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document" id="myModalDocument">
        @Html.Partial("_editItem", new nMvMmenuItem())
    </div>
</div>

但是,当控制器动作期望AjaxResquest时,控制器将Request.IsAjaxRequest()评估为false。

 public async Task<ActionResult> editItem(int? id)
    {
        if (Request.IsAjaxRequest())
        {                
            return PartialView("_editItem", await db.nMmenuItems.FindAsync(id));
        }
        return View();
    }

这会刷新整个视图并阻止模态工作。

我在_Layout.cshtml页面中捆绑了以下脚本:

"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery-ui-{version}.js",
"~/Scripts/jquery.unobstrusive*",
"~/Scripts/jquery.validate",
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"

谢谢你的帮助!

唐吉jo德

我发现将Submit()附加到表单的onclick事件不会执行ajax请求。因此,我的解决方案是删除Ajax.SubmitForm,而是处理我的js中的click事件:

更新后的视图如下所示:

<div class="nMmenuItem">
<form method="get" action="@Url.Action("editItem","nMrestaurant",new { id = Model.ID })"
      data-nM-ajax="true" data-nM-target="#myModalContent">
    <div>            
        <div class="text-center">
            @Model.name
        </div>            
    </div>
</form>

在js中,我将表单提交绑定到父div的click事件:

$('.nMmenuItem').click(ajaxFormSubmit);

以及处理表单提交并打开结果模式对话框的函数:

var ajaxFormSubmit = function () {        

    var $form = $(this).children('form:first');

    var options = {
        url: $form.attr("action"),
        type: $form.attr("method"),
        data: $form.serialize()
    };

    $.ajax(options).done(function (data) { 
        var $target = $($form.attr("data-nM-target"));                     
        $target.replaceWith(data);
        $("#myModal").modal(dialogOpts);
    });

    return false;

};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

WebGrid 列中的 ASP.NET MVC Html.BeginForm

Html.BeginForm (ASP.NET MVC) 的问题

ASP.NET MVC:使用Ajax.BeginForm的多个提交按钮

ASP.NET MVC:如何更改页面加载后Ajax.BeginForm将发布到的位置?

将Ajax.BeginForm与ASP.NET MVC 3 Razor一起使用

在ASP.NET MVC中提交Ajax.BeginForm之后,jQuery代码无法正常工作

ASP.NET MVC 5 - ajax.beginform() 带空参数

ELMAH不会记录从ASP.NET MVC Ajax.BeginForm引发的异常

html.beginForm通过ASP> NET-MVC5中的jQuery Ajax函数将List <强类型输入>的空值发布到控制器

ASP.net MVC4中Html.BeginForm中的多个按钮中的问题

如何将ID属性添加到ASP.NET MVC中的Html.BeginForm()?

ASP.NET MVC在Html.BeginForm提交之前动态保存数据

ASP.NET MVC BeginForm无法使用参数发布

ajax beginform mvc回调

@ Html.BeginForm()如何工作?和搜索结果在Microsoft ASP.Net MVC 5教程中?

通过AJAX发送对象数组-ASP.NET MVC

jQuery Ajax发送数据-ASP.Net MVC

在ASP MVC视图中使用ajax.beginform更新div

ASP MVC - 一次 Ajax.BeginForm 单击许多请求

为什么我们使用ajax。在MVC中的BeginForm代替添加表单标签和Html.BeginForm

ASP.NET MVC-使用BeginForm时导致此文本出现的原因是什么?

在 ASP.NET 中使用带有 Ajax.BeginForm 的 Javascript Bootbox

向 Ajax.BeginForm 发送失败的正确方法是什么?

MVC4 Ajax.BeginForm routeValues转换为HTML中的类型名称

带有按钮单击的 Html Beginform 在 .net core mvc 中不起作用

Ajax.BeginForm在Chrome MVC 5中不起作用

为什么在MVC中使用Ajax.BeginForm

带有MVC的Ajax.BeginForm不会起作用

在Html.BeginForm MVC之外提交表单