Ajax.BeginForm不能正常工作 [英] Ajax.BeginForm doesn't work properly

查看:122
本文介绍了Ajax.BeginForm不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题阿贾克斯提交。我有一个主查看,我呈现 PartialView ,最后一个我加载另一个内部 PartialView 。事情是这样的:

I have a problem with Ajax submit. I have a main View where I render a PartialView and inside the last one I load another PartialView. Something like this:


  • 主视图

  • Main view


  • 元素的列表 - > PartialView 1


  • 创建新的元素 - 在 PartialView PartialView 2> 1

  • Create new element -> PartialView 2 inside PartialView 1

我使用 AjaxBeginForm 将其替换和更新选项:

I am using AjaxBeginForm with replace and update options:

 @using (Ajax.BeginForm("Create", "MyController", 
      new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "form0", HttpMethod = "POST" }))

我的问题是,提交的作品非常好第一次。它节省了新的元素和重新渲染 PartialView 1(它更新我的列表)。如果想再次提交,将我重定向到 PartialView 1

My problem is that submit works very well first time. It saves the new element and re-render the PartialView 1 ( it updates my list ). If want to submit again it will redirect me to PartialView 1.

为什么发生的,什么是错在我的code?我该怎么办呢?

Why is happening that and what is wrong in my code? How can I do it ?

下面是我的控制器动作

    [HttpPost]
    public PartialViewResult Create(Model viewModel)
    {
        viewModel.Save(viewModel.FormModel);

        var newViewModel = new DefaultViewModel(viewModel.xID,viewModel.yID);

        return PartialView("_DefaultPartialView", newViewModel);

    }

这是确定我的行动返回 PartialView ?它应该是类型 JsonResult

PartialView 1

And PartialView 1:

@using (Ajax.BeginForm("CreateBehaviorLog", "BehaviorLog", new AjaxOptions {    InsertionMode = InsertionMode.Replace, UpdateTargetId = "form0", HttpMethod = "POST" }))
{

@model DefaultViewModel

@Scripts.Render("~/bundles/jquery")

<h2>Title</h2>

 { Html.RenderPartial("PartialView2",    Model.ModelForPartialView2); }

<div id="listOfELements">

 @foreach(var item in Model.X)
  {
     --list--
  }  
</div>
}

感谢您。

更新:

我解决了这个问题(这是一个非常新手的错误)。我会后,明天我的答案,因为现在它的有点晚了,我需要睡觉!

I fixed this ( it was a very newbie mistake ). I'll post tomorrow my answer because now it's kinda late and I need to sleep!

推荐答案

所以,首先,当你做出一个Ajax调用确定你包括你需要的一切。在这里,我的意思是:

So, first of all, when you make an Ajax call be sure you included everything you need. Here I mean:


  • 请务必将&LT;添加键=UnobtrusiveJavaScriptEnabled
    值=真/方式&gt;
    在webconfig

  • Be sure to have <add key="UnobtrusiveJavaScriptEnabled" value="true"/> in your webconfig.

请确认您已包含在页面的脚本。

Be sure you have included the script in the page.

如果您使用的是最新的的jQuery 您需要更改生活使用功能不显眼的剧本

If you are using the latest jQuery you need to change live function with on in your unobtrusive script.

顺便说一句,如果你有上面这里所说的是什么问题,我做了第一次(现在是改变,因为它不是这样一个很好的解决方案):

Btw, if you have the problem mentioned above here is what I have done first time ( it is changed now because it's not such a good solution ) :

 MainView

 @using (Ajax.BeginForm("Create", "MyController", 
  new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "form0", HttpMethod = "POST" }))
    {
     RenderPartial1

         RenderPartial1.2
     }


     RenderPartial2
     ..
     RenderPartialN

正如你所看到的,我已经把 AjaxBeginForm 之外 Partial1.2 从我提交的MainView 。这种方法不太好,因为如果你需要2种形式你会做什么呢?

As you can see, I've put AjaxBeginForm outside the Partial1.2 from which I submit, in the MainView. This method wasn't so good because if you need 2 forms what will you do then ?

在最后,我用 AjaxBeginForm 和使用 HtmlBeginForm 阿贾克斯后的JavaScript

In the end I quit using AjaxBeginForm and used HtmlBeginForm with ajax post from javascript.

这篇关于Ajax.BeginForm不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆