public ActionResult CheckNumber(string number) {
if (List !=0) {
return View("Index");
}
else {
return View("False");
}
}
I have a CheckNumber
method like above. I want to write an Ajax on view to call this method. If if (List !=0)
will call Index()
method. How can I do that?
My Index()
method:
[HttpPost]
public async Task<ActionResult> Index(tempUser userDetails) {}
$.ajax({
method: "POST",
url: '/controller/CheckNumber',
data: {number},
success: fucntion(result){
if(result.redirect === 'Index'){
location.replace('/controller/Index')
}
}
})
And in controller u need to pass obj like this
if (List !=0) {
return Json(new {redirect = true}, JsonRequestBehavior.AllowGet)
}
Of course u can modify this code to work how u want to, but this is example how to use Ajax call
I like Serlok's method. I use a similar approach on projects quite often. I don't use it to redirect pages, but I send responses back often for various alerts. I just prefer since I've already sent the request to the server, why send a response back to the calling ajax method? Just redirect to another view right there.
This way is just a different alternative. You use the controller to RedirectYourAction. Another controller, which will fire your view. This way has worked well for me
var data = JSON.stringify({ 'number': userInput});
$.ajax({
method: "POST",
url: '/controller/CheckNumber',
contentType: 'application/json',
data: data,
success: function(response){
console.log(response);
//You could do your redirect here.
}
}
})
public ActionResult CheckNumber(string number)
{
int List = Convert.ToInt32(number);
if (List != 0) //You have List here, you sure you don't mean number? Or you're instantiating a list somewhere..
{
RedirectToAction("Index"); //Takes you to your index controller and the view associated with it. Same as below.
}
else
{
RedirectToAction("SomeOtherControllerName");
}
return View();//default controller view.
}
You can use bottom link , it's a article for some easy example about your issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.