Reputation: 1122
I'm using the following jQuery to submit and save data:
var data = "Sample data";
$.post('@Url.Content("~/Student/SaveData")', { Data : data}
//I want to alert SUCCESS or ERROR here
This is my controller:
public ActionResult SaveData(string data)
{
try
{
//Add logic to save data
//pass SUCCESS indication.
}
catch(Exception ex)
{
//pass ERROR indication
}
return View();
}
I want to alert success message if data saved successfully otherwise error message. How to handle it in jQuery section?
Upvotes: 0
Views: 626
Reputation: 62488
you are firstly using wrong helper instead of @Url.Content
you need to use @Url.Action
which generates url for you action:
and use $.ajax
like this:
var data = "Sample data";
$.ajax({
url: '@Url.Action("SaveData","Student")',
type: "get",
data: { sample:data}
success: function (res) {
alert("success");
},
error: function () {
alert("failure");
}
});
and your action would be like:
public ActionResult SaveData(string sample="")
{
return Content(sample);
}
Upvotes: 1