Reputation: 3823
I have a specific problem where I post data to my MVC action in controller like this:
$(".btnAnalyze").click(function () {
if (jQuery.isEmptyObject(product_ids) == true) {
alert("Array is empty");
}
else {
var postData = { values: Object.keys(product_ids) };
$.ajax({
type: "POST",
url: "/Analyze/Index",
data: postData,
dataType: "json",
traditional: true
});
}
});
And this is my action:
[HttpPost]
public ActionResult Index(List<string> values)
{
List<string> Products = new List<string>();
foreach (var id in values)
{
Products.Add(GetAllProductByID(id));
}
return View("Index");
}
For some reason the part:
"Return view("Index") doesn't renders the view I said to render...
Any ideas how to fix this ?
Upvotes: 1
Views: 3550
Reputation: 15155
This is not going to simply refresh the view. You are calling the controller with an ajax call and this does not re/render the view, it simply returns the view as html. Can you inspect the result of that ajax call? If you test your endpoint in postman, soapui, fiddler or your browser's F12 debugger then you should see what is coming back from that ajax call. I would look into the AjaxBeginForm as an alternative to BeginForm
.
Also, if you only want to do something with the data returned then simply return Json as in
return Json(Products, JsonBehavior.AllowGet);
Upvotes: 1