如何使用Ajax.BeginForm MVC佣工JSON结果? [英] How to use Ajax.BeginForm MVC helper with JSON result?

查看:75
本文介绍了如何使用Ajax.BeginForm MVC佣工JSON结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用ASP.NET MVC Ajax.BeginForm帮手,但不希望在调用完成时要使用的现有内容插入选项。相反,我想用一个自定义JavaScript函数作为回调。

I'm trying to use the ASP.NET MVC Ajax.BeginForm helper but don't want to use the existing content insertion options when the call completes. Instead, I want to use a custom JavaScript function as the callback.

这工作,但结果我想应该返回JSON。不幸的是,该框架只是把该数据作为一个字符串。下面是客户端code。服务器code只是返回一个JsonResult有一个字段,UppercaseName。

This works, but the result I want should be returned as JSON. Unfortunately, the framework just treats the data as a string. Below is the client code. The server code simply returns a JsonResult with one field, UppercaseName.

<script type='text/javascript'>
    function onTestComplete(content) {
        var result = content.get_data();
        alert(result.UppercaseName);
    }
</script>

<% using (Ajax.BeginForm("JsonTest", new AjaxOptions() {OnComplete = "onTestComplete" })) { %>
    <%= Html.TextBox("name") %><br />
    <input type="submit" />
<%} %>

而不是显示大写的结果,这是不是显示不确定的。 content.get_data()似乎举行的JSON,但只在字符串形式。我怎么去这个转换为一个对象?

Instead of showing the uppercase result, it is instead showing undefined. content.get_data() seems to hold the JSON, but only in string form. How do I go about converting this to an object?

这一切似乎有点令人费解真的。有没有更好的方式来获得在使用Ajax.BeginForm得到的内容?如果是这样辛苦,我可以完全跳过Ajax.BeginForm,只是使用jQuery的表单库。

All of this seems a bit convoluted really. Is there a better way to get at the resulting content using Ajax.BeginForm? If it's this hard, I may skip Ajax.BeginForm entirely and just use the jQuery form library.

推荐答案

试试这个:

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

这会给你造成JSON格式,你可以使用 json_data [0] 来获得的第一个记录

this will give you result in JSON format and you can use json_data[0] to get the first record

这篇关于如何使用Ajax.BeginForm MVC佣工JSON结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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