简体   繁体   English

MVC:在Ajax调用中从控制器返回时,结果不确定

[英]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.

相关问题 Ajax-Call从Asp.Net MVC中的控制器(返回IList)获取JSON结果总是错误 - Ajax-Call to get JSON Result from Controller (returning IList) in Asp.Net MVC always errors 为什么是我的清单 <string> 不从$ ajax调用从MVC控制器返回 - Why is my List<string> not returning from MVC Controller from $ajax call 从控制器获取结果但 ajax 调用获取错误而不是在 MVC5 和 C# 中成功 - Getting the result from controller but ajax call fetching error instead of success in MVC5 and C# ASP.NET MVC AJAX 调用 Controller 不返回任何数据 - ASP.NET MVC AJAX Call to Controller Not Returning any Data 尝试将文件发送到MVC中的控制器时,AJAX返回undefined - AJAX returns undefined when trying to send File to controller in MVC 如果从另一个视图的 ajax 调用启动,控制器不会返回视图 - Controller not returning view if initiated from ajax call from another view 在使用MVC托管时,无法通过AJAX调用WebAPI Controller - Unable to call WebAPI Controller through AJAX, when hosted with MVC 如何从Ajax调用获取数据到MVC控制器? - How to get data from ajax call to MVC controller? 从ajax调用控制器函数返回“__RequestVerificationToken”不存在(果园)mvc - Call controller function from ajax returns “__RequestVerificationToken” is not present(orchard) mvc C#MVC Ajax结果未定义 - C# MVC Ajax result undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM