Form + Ajax无法正常运行

时间:2021-09-19 12:29:53

Form+Ajax -> Results on exact DIV 3 hours, 53 minutes ago|LINK

Form + Ajax - >结果精确DIV 3小时,53分钟前|链接

Hi,

I need to visualize my partialView (results after a form submission) in a DIV.

我需要在DIV中可视化我的partialView(表单提交后的结果)。

I have the following files:

我有以下文件:

View Man_por.cshtml

...

<div class="col-md4" id="divTabPortfolios">

enter code here@{Html.RenderAction("Load_portfoliosPartial","Management_portfolios");}

<div>

<div class="col-md4" id="divTabPortfolio">

<div>

...

View Load_portfolios_partial

<script src="myscript.js" type="text/javascript"></script>

....

...

@using ( @ Html.BeginForm() )

{

....

...

<button type="submit" class=".." value="Open"/>

}

Script MyScript.js

$(function () {
    $('form').submit(function () {
        if ($(this).valid()) {
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    $('#divTabPortfolio').html(result);
                }
            });
        }
        return false;
    });
});

When it shows the result it open entire page and not a partial view (_layout.cshtml+Page) etc.. and it doesn't update the specified DIV.

当它显示结果时它打开整个页面而不是局部视图(_layout.cshtml + Page)等。它不会更新指定的DIV。

How could i solve it?

我怎么能解决它?

1 个解决方案

#1


0  

Use @using (Ajax.beginForm()) instead of @using (Html.beginForm().

使用@using(Ajax.beginForm())而不是@using(Html.beginForm()。

MVC supports Ajax forms out-of-the-box.

MVC支持开箱即用的Ajax表单。

You then provide the action, controller and selector (UpdateTargetId is set to the id of the panel to update) like this:

然后,您提供操作,控制器和选择器(UpdateTargetId设置为要更新的面板的ID),如下所示:

<div id="divTabPortfolio">
    @using (Ajax.BeginForm("SomeAction", "YourController", new { id = optionalParameters }, new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "divTabPortfolio" }))
    { 
    }
</div>

No need for your own jQuery/Ajax for this sort of basic Ajax post-backs in MVC/Razor.

对于MVC / Razor中的这种基本Ajax回发,不需要你自己的jQuery / Ajax。

So long as your action method returns a PartialView you don't need to do anything else.

只要您的操作方法返回PartialView,您就不需要执行任何其他操作。

As suggested, if you could post more of the page it would help provide specifics

正如所建议的那样,如果你可以发布更多的页面,它将有助于提供细节

#1


0  

Use @using (Ajax.beginForm()) instead of @using (Html.beginForm().

使用@using(Ajax.beginForm())而不是@using(Html.beginForm()。

MVC supports Ajax forms out-of-the-box.

MVC支持开箱即用的Ajax表单。

You then provide the action, controller and selector (UpdateTargetId is set to the id of the panel to update) like this:

然后,您提供操作,控制器和选择器(UpdateTargetId设置为要更新的面板的ID),如下所示:

<div id="divTabPortfolio">
    @using (Ajax.BeginForm("SomeAction", "YourController", new { id = optionalParameters }, new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "divTabPortfolio" }))
    { 
    }
</div>

No need for your own jQuery/Ajax for this sort of basic Ajax post-backs in MVC/Razor.

对于MVC / Razor中的这种基本Ajax回发,不需要你自己的jQuery / Ajax。

So long as your action method returns a PartialView you don't need to do anything else.

只要您的操作方法返回PartialView,您就不需要执行任何其他操作。

As suggested, if you could post more of the page it would help provide specifics

正如所建议的那样,如果你可以发布更多的页面,它将有助于提供细节