Alert on Save Click in MVC3
w开发者_StackOverflow中文版hen i click on the save button. I want javascript Alert come that "data saved Sucessfully". How i do this in MVC3.. thanks
You can send a json result back from the server with a success or fail message. Then simply on the complete method of an ajax request, read your json result and display the message accordingly.
Your client side script will look like something like this (if using jQuery):
$("#SubmitBtnId").click(function() {
$.ajax({
type: "POST",
url: "controller/action",
data: "name=FormName&location=Florida",
complete: function(data){
if(data.Success) { alert(data.Message); }
}
});
});
Make sure you return from your controller a Json result that includes a Success property. You can do this like
return Json(new
{
Success = true,
Message = "Data saved Successfully"
});
You can pass a flag in the ViewBag to the View to say the data is successfully saved. In the view then you can trigger a javascript variable which will in turn trigger the alert. Hope this helps.
精彩评论