I found this problem more than once on SO, but I found no solution that works for me.
I have a view:
<div id="profpics">
@Html.Partial("_ProfileImagesPartial", Model.ProfileImages)
@using (Ajax.BeginForm("UploadImage", "ProfileImages", new AjaxOptions { UpdateTargetId = "profpics", HttpMethod = "GET" }))
{
@Html.AntiForgeryToken()
<input type="file" name="file" id="imgInp" />
<img id="blah" src="#" alt="your image" />
<input type="submit" value="OK" />
}
</div>
In my controller I have:
[HttpPost]
public PartialViewResult UploadImage(HttpPostedFileBase file)
{
List<ProfileImage> images = new List<ProfileImage>();
images = db.Images.ToList();
return PartialView("_ProfileImagesPartial",images);
}
My controller does receive the call, It does the required actions, calls the partial view and tries to fill it. But then, instead of returning to my view and update the div, it redirects to the controller and action I used in my request (/UploadImage) and shows me the partial view there instead of returning to my view and updating the div. The controller action I call isn't the same as the controller where my partial view lives in, but that too, I think should be no problem?
I found a lot of answers saying there are absent scripts, but i use:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
in my _Layout.cshtml and updated the jqueryval to contain unobtrusive validation:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
I also tried to add them manually:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
But that too didn't help.
Does anyone know what It could be?
Edit: I found out my ajax form isn't really working. I does a POST while I state GET in the ajax form. When I put them both to POST OR GET I do get the right request, but the fact is, my request is no Ajax request. So I guess I Always get redirected because the UpdateTargetID is not being used.
These are the script included on my page:
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
what could be wrong please?