Here is my ajax call
$.ajax({
type: "POST",
url: "MyMethod",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ Param1: 'value1', Param2: 'value2'}),
success: function (msg) { location.href = '@Url.Action("MyActionMethod", "MyController")'; },
error: function (msg) { alert(msg); }
});
This is my model
public class MyModel
{
public string Param1 { get; set; }
public string Param2 { get; set; }
}
MyMethod implementation is as follows
public bool MyMethod(MyModel model)
{
if (!ValidateModel(model) )
{
// I want to error a descriptive error
return false;
}
// Some other processing
return true;
}
Problem is that I don’t know how does it determine if call is a success or failure. Returning ture/false does not seem to help as code always go in the success code path (that is it redirects to MyController.MyActionMethod). Any ideas what i am doing wrong
This is usually what I do:
Note the change in the return method from
booltoJsonResult. And then in your JavaScript you could test the properties coming through on the JSON object: