开发者

Ajax.BeginForm different behaviour between MVC2 and MVC3?

开发者 https://www.devze.com 2023-03-12 11:41 出处:网络
I have a piece of code working in MVC2, but after I upgraded to MVC3 it does not work any more. In my view I have

I have a piece of code working in MVC2, but after I upgraded to MVC3 it does not work any more.

In my view I have

<% using (Ajax.BeginForm("MyAction", new AjaxOptions { HttpMethod = "Post",
 OnComplete = "MyAction_OnComplete" })) { %>
my controls here...
<% } %>

<script type="text/javascript">
        function MyAction_OnComplete(content) {
                var output = eval(content.get_response().get_object());
        }
</script>

My action method returns JSON object:

[HttpPost]
public ActionResult MyAction(MyViewModel data) {
    // result = new myObje开发者_开发技巧ct(); ....
    return new JsonResult { ContentEncoding = Encoding.UTF8, Data = result };
}

I have updated JavaScript reference as

<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery.validate.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery.unobtrusive-ajax.js") %>"></script>

in web.config I have turned on Unobtrusive JavaScript

<appSettings>
    <add key="webpages:Version" value="1.0.0.0"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

In MVC2, my client side oncomplete event handler can interpret JSON returned from action and perform further job, but that line of code in MVC3 breaks without being able to parse the data.

Any idea, what is wrong with my code?

Thanks Hardy


I think you have the same problem as described here: http://forums.asp.net/t/1636706.aspx/1?MVC3+Bug+using+get_response+get_object+

To summarize: use the OnSuccess event of Ajax.BeginForm instead, and your parameter in the event handler will be the object already (you won't have to call any get_response/get_object unwrapping methods).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号