.net mvc ajax重定向,c# - How to get an ASP.NET MVC Ajax response to redirect to new page instead of ins...

I m not satisfied by the best answer by the Joseph, instead of fixing the correct problem, he told that this is wrong use case. In fact there are many places for example if you are converting an old codebase to ajaxified code and there you NEED it, then you NEED it. In programming there is no excuse because its not only you who is coding its all bad and good developers and you have to work side by side. So if I don't code redirection in ajax my fellow developer can force me to have a solution for it. Just like I like to use all AMD patterned sites or mvc4, and my company can keep me away from it for a year.

So let's talk on the solution now.

I have done hell heck of ajax request and response handling and the simplest way I found out was to send status codes to the client and have one standard javascript function to understand those codes. If i simply send for example code 13 it might meant a redirect.

So a json response like { statusCode: 13, messsage: '/home/logged-in' }

of course there are tons of variations proposed like

{ status: 'success', code: 13, url: '/home/logged-in', message: 'You are logged in now' }

etc , so up to your own choice of standard messages

Usually I Inherit from base Controller class and put my choice of standard responses like this

public JsonResult JsonDataResult(object data, string optionalMessage = "")

{

return Json(new { data = data, status = "success", message = optionalMessage }, JsonRequestBehavior.AllowGet);

}

public JsonResult JsonSuccessResult(string message)

{

return Json(new { data = "", status = "success", message = message }, JsonRequestBehavior.AllowGet);

}

public JsonResult JsonErrorResult(string message)

{

return Json(new { data = "", status = "error", message = message }, JsonRequestBehavior.AllowGet);

}

public JsonResult JsonRawResult(object data)

{

return Json(data, JsonRequestBehavior.AllowGet);

}

About using $.ajax intead of Ajax.BeginForm

I would love to use Jquery ajax and I do, but again its not me in the whole world to make decisions

I have an application full of Ajax.BeginForm and of course I didnt do that. But i have to live with it.

So There is a success callback in begin form too, you don't need to use jquery ajax to use callbacks

Something about it here

Ajax.BeginForm, Calls Action, Returns JSON, How do I access JSON object in my OnSuccess JS Function?

Thanks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值