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..
Any form (AJAX or otherwise) will default to the current page's URL if no action is explicitly given. You can use an overload of the BeginForm
method to specify an action:
Ajax.BeginForm("Code", new AjaxOptions { ... })
Regarding the warning about synchronous AJAX, that's simply telling you that the behavior is deprecated. Somewhere (I'm not sure where) you're telling the AJAX operations to be invoked synchronously. The "A" in AJAX stands for "Asynchronous". The browser is simply telling you that your AJAX should be asynchronous. Which it should be.
Wherever the code is instructing AJAX calls to be synchronous (I don't see anything here which is doing that, so it's probably somewhere else) should be changed.