ASP.NET MVC with JQuery $post and parameter
What is wrong with this code. The code is finding the javascript and debug1 shown. If I remove the parameter p the code also founds mycontrol action and debug2 is shown.
View:
function method(p) {
alert("debug1");
$.post('../MyController/MyAction/' + p, function() {
alert("debug2");
$('#panel').empty().html开发者_如何学Go('<img src="../Content/images/ajax-loader.gif" / >');
$('#panel').load('../Controller/Index');
});
}
Controller:
public ActionResult MyAction(int p)
{
// Some code
return null;
}
Seems to me like you have a problem with your routes. You can't change the name of your parameter if you don't change your routes. A route that would work for your scenario is:
routes.MapRoute("MyRoute",
"MyController/MyAction/{p}",
new { controller = "MyController", action = "MyAction", p = "" }
);
Just change it like this:
$.post('../MyController/MyAction/p=' + p, function() {
That function is a callback and will get called regardless of success or failure. If you're calling a webservice, try including a success = true/false property as part of your result. That way you can do the following
`$.post('../MyController/MyAction/' + p, function(result) {
if (!result.success) return;
alert("debug2");
$('#panel').empty().html('<img src="../Content/images/ajax-loader.gif" / >');
$('#panel').load('../Controller/Index');
});`
精彩评论