2

I have this controller:

public ActionResult Index()
    {

        return View();
    }

    [HttpGet]
    public PartialViewResult Code()
    {
        return PartialView("Code");
    }

    [HttpPost]
    public PartialViewResult Code(string code)
    {

        return PartialView("Code");
    }

I have call partial in my Index view

@Html.Partial("Code")

and here's my partial view

@model Kubeti.Models.Codes



@using (Ajax.BeginForm(new AjaxOptions { HttpMethod="POST", UpdateTargetId = "result", InsertionMode=InsertionMode.Replace }))
{
@Html.EditorFor(x => x.code)
@Html.ValidationMessageFor(x => x.code)

<input type="submit" value="OK" />
}

<div id="result" style="width: 500px; height:500px; border:1px solid red;">
</div>

Of course I have jquery and unobtrusive.js in my Layout

@RenderBody()

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

But when I'm clicking on submit it goes in my index action (I'm debugging) instead of my PartialViewResult Code. What's wrong with it?

In console there's this warning:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

I google it but can't find answer which will customize to my problem..

4

1 回答 1

2

如果没有明确给出任何操作,任何形式(AJAX 或其他)都将默认为当前页面的 URL。您可以使用方法的重载BeginForm来指定操作:

Ajax.BeginForm("Code", new AjaxOptions { ... })

关于同步 AJAX 的警告,这只是告诉您该行为已被弃用。某处(我不确定在哪里)您告诉要同步调用 AJAX 操作。AJAX 中的“A”代表“异步”。浏览器只是告诉您 AJAX 应该是异步的。它应该是。

无论代码在哪里指示 AJAX 调用是同步的(我在这里看不到任何这样做的东西,所以它可能在其他地方)都应该更改。

于 2015-02-28T22:17:34.147 回答