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 ?
dataType: "json"is telling the ajax client to expect JSON back, but you're trying to return HTML, and b) are you sure you don't wantreturn PartialView()? You wouldn't normally want to return a whole view via ajax - it would try to return all the Layout template as well, which should already be in the page.Indexview, then do not use ajax. If you want to update part of the existing page, then changedataType: "json",todataType: "html",and add a success callback to update the DOM -success: function(response) { $(someElement).html(response); }