ASP.NET MVC3 - 使用JavaScript错误 [英] ASP.NET MVC3 - Bug using Javascript

查看:171
本文介绍了ASP.NET MVC3 - 使用JavaScript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Ajax.BeginForm()来员额从我的控制器的JSON的结果(我使用MVC3)。当JSON结果被称为它应该被送到一个JavaScript函数,并使用提取物

I'm trying to use Ajax.BeginForm() to POST A Json result from my controller (I'm using MVC3). When the Json result is called it should be sent to a javascript function and extract the object using

var myObject = content.get_response().get_object();

不过它只是抛出一个微软JScript运行时错误:对象不支持此属性或方法试图调用Ajax的帖子的时候

However it just throws a "Microsoft JScript runtime error: Object doesn't support this property or method" when trying to invoke the Ajax POST.

我的code:

控制器:

[HttpPost]
public ActionResult Index(string message)
{
    return Json(new { Success = true, Message = message });
}

查看:

<!DOCTYPE html>

<html>
<head>
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>

    <script type="text/javascript">
        function JsonAdd_OnComplete(mycontext) {
            var myObject = mycontext.get_response().get_object();
            alert(mycontext.Message);
        }
    </script>
</head>

<body>
    <div>
        @using(Ajax.BeginForm("Index", "Home", new AjaxOptions() { HttpMethod = "POST", OnComplete = "JsonAdd_OnComplete" }))
        {
            @Html.TextBox("message")

            <input type="submit" value="SUBMIT" />

        }
    </div>
</body>
</html>

但奇怪的是完全相同的code工作在MVC2 - 这是一个错误,或者有我忘了什么事

The strange thing is that the exactly same code works in MVC2 - Is this a bug, or have I forgot something?

在此先感谢。

推荐答案

AFAIK在ASP.NET MVC 3 RC MS AJAX有pcated赞成的jQuery是德$ P $是由所有阿贾克斯* 辅助方法。 JavaScript有成为不显眼的的为好。这意味着,你不再需要调用 .get_response()的get_object()只是简单:

AFAIK in ASP.NET MVC 3 RC MS AJAX has been deprecated in favor of jQuery which is used by all Ajax.* helper methods. Javascript has become unobtrusive as well. This means that you no longer have to call .get_response().get_object() but simply:

function JsonAdd_OnComplete(myObject) {
    // here myObject is already the JSON object 
    // as returned by the controller action
    alert(myObject.Message);
}

这篇关于ASP.NET MVC3 - 使用JavaScript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆