I have an MVC project in which I have a form with a submit button. I have added a client side handler jquery that catches the form submit event. The javascript function invokes the same MVC action that would be invoked without javascript.
$("form[action ='/List/CreateItem']").submit(
function() {
$.post($(this).attr("action"), $(this).serialize(), function(response) { $("#results").html(response); });
return false;
}
);
In the called MVC action, I test Request.IsAjaxRequest to decide whether to return a view or JSON result. My problem is that Request.IsAjaxRequest returns false, although I know that the call is made from the jquery function. (I know this because if I comment out the $ .post line in the jquery function and just leave the returned false line, nothing will happen. If I don't comment on the line, the action will be executed, but it will return the view because IsAjaxRequest is false. )
Should this line force Request.IsAjaxRequest to be true?
Grant source
share