[英]MVC: When returning from the controller on Ajax call, the result is undefined
I'm making an Ajax call to the controller when clicking on a Kendo button and return the model: 单击Kendo按钮并返回模型时,我正在对控制器进行Ajax调用:
@(Html.Kendo().Button()
.Name("btnSubmit")
.HtmlAttributes(new { type = "button" })
.Icon("rotate")
.Content("View Details"))
<script>
$("#btnSubmit").click(function () {
$.ajax({
url: "/MyController/MyMethod/",
type: 'post',
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (result) {
window.location.href = "@Url.Action("RedirectToView", "MyController", new { myModel = "data" })".replace("data", result);
}
})
});
</script>
The controller's method returns the model: 控制器的方法返回模型:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult MyMethod()
{
var reportDate = Session["FileDate"] == null ? DateTime.Now : Convert.ToDateTime(Session["FileDate"].ToString());
var myModel = new MyModel();
myModel.ColOfData = myModel.GetColOfData(reportDate);
return Json(myModel, JsonRequestBehavior.AllowGet);
}
When I debug my Ajax
function, result is undefined. 当我调试
Ajax
函数时,结果是不确定的。 The result should be assigned to MyModel, since I'm returning the model back to an Ajax
function. 结果应该分配给MyModel,因为我要将模型返回给
Ajax
函数。 I need to pass that result to another method in the controller that would return my Partial View
containing the Grid: 我需要将该结果传递到控制器中的另一个方法,该方法将返回包含网格的
Partial View
:
public ActionResult RedirectToView(MyModel myModel)
{
return PartialView("_MyPartialView", myModel);
}
What am I doing wrong? 我究竟做错了什么?
Your problem isn't related to kendo. 您的问题与剑道无关。
From your controller you have to return a json object like this 从您的控制器,您必须返回一个json对象,像这样
return Json(new {result=myModel});
And in your ajax result you will have your entire model. 在ajax结果中,您将拥有整个模型。
After that from the code you provided I am afraid you can't pass your entire model in the url of your GET. 之后,您提供的代码恐怕无法在GET的URL中传递整个模型。
You could probably pass the model Id like that 您可能会像这样传递模型ID
window.location.href = "@Url.Action("RedirectToView", "MyController", new { id= "modelId" })".replace("modelId", result.Id);
And make your action something like that 让你的动作像那样
public ActionResult RedirectToView(string id){
// Get the model data you want .....
return PartialView("_MyPartialView", myModel);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.