【发布时间】:2014-05-12 18:39:18
【问题描述】:
在我的View 中,我有一个带有微调器的功能区,用户可以在其中选择int 类型,我将其用作参数以在存储库中获取一些数据。
于是我提出了一个ajax请求:
var url = "/ControllerExample/GetJsonUrl?param=" + s.GetItemValueByName("countParam") ;
jQuery.ajax({
type: "GET",
url: url,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
window.location.href = data;
},
failure: function (errMsg) {
alert(errMsg);
}
});
在我的 Controller 中,我有一个 ActionResult,它返回一个带有特定 URL 的 JsonResult 到“成功”函数:
public ActionResult GetJsonUrl(string param)
{
var urlHelper = new UrlHelper(ControllerContext.HttpContext.Request.RequestContext);
string url = urlHelper.Action("OtherAction", "ControllerExample", new {id = param});
return new JsonResult {Data = url, JsonRequestBehavior = JsonRequestBehavior.AllowGet};
}
所以如果结果是success,请求返回ajax成功函数,然后到ExampleController执行OtherAction .
我想只用一个动作就可以做到,可以吗?刷新页面在JsonResult什么的。
【问题讨论】:
-
您可以使用模型将该视图呈现为字符串并使用 JSON 结果传递。
标签: jquery ajax asp.net-mvc json