I have an ASP.net 4.5, MVC5 project that I have open in Visual Studio 2015. My Ajax.BeginForm doesn't want to update the div like it should but opens up in a new tab.
This question been posted many times and the answer seems to always be to add microsoft's jquery unobtrusive library. However I have added it and the ajax form still opens in a new tab instead of the current partial page section.
Here is my code in my cshtml
In cshtml
@Html.Partial("_UploadFile")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryajax")
}
In BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/jqueryajax").Include(
"~/Scripts/jquery.unobtrusive-ajax*"));
jquery.unobtrusive-ajax is added to project via nuget
In Scripts folder jquery.unobtrusive-ajax.js jquery.unobtrusive-ajax.min.js
In Packages.config
<package id="jQuery" version="1.10.2" targetFramework="net45" />
<package id="jQuery.UI.Combined" version="1.11.4" targetFramework="net45" />
<package id="jQuery.Validation" version="1.11.1" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net45" />
Partial Page (_UploadFile.cshtml):
@using (Ajax.BeginForm(
"UploadFile",
null,
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "uploadfile1"
},
new { id = "UploadForm", enctype = "multipart/form-data", data_ajax = "false" }
))
{
@Html.AntiForgeryToken()
<div id="uploadfile1">
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="file" name="file" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Upload" class="btn btn-sm" />
</div>
</div>
</div>
</div>
}
Take note I had to add data_ajax = "false" otherwise I would get a jquery error if jquery.unobtrusive-ajax.js is linked on the page.
In my Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadFile(HttpPostedFileBase file)
{
return PartialView("_UploadFile");
}
I tried putting jquery.unobtrusive-ajax in different places in the page but this file doesn't seem to be the issue.
- Is there any way to troubleshoot this unwanted open in new tab behavior? It feels like pissing in the dark troubleshooting it.
- What other reasons could cause this behaviour apart from jquery.unobtrusive-ajax.js not being referenced in the page?