POST的Json没有模型和Ajax [英] POST Json without model and Ajax

查看:136
本文介绍了POST的Json没有模型和Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我只想用HTTP POST发送JSON来asp.net MVC2控制器。

For now, I just want to use HTTP POST to send json to asp.net mvc2 controller.

由于JSON实际上是JSON对象的列表,和他们每个人都有不同的领域和放大器;长,所以我很难做出了一个输入模型。

Since the JSON is actually a list of JSON objects, and each of them has different fields & length, so it hard for me to make up a input model.

所以我想知道有没有办法,我没有张贴模型/ AJAX JSON在ASP.NET MVC2?

So I want to know is there a way for me to post JSON without model/ajax to a controller in ASP.NET MVC2?

推荐答案

@布赖恩的答案建立在,我​​们做正是这一点。请记住,这是jQuery的1.4.2

Building on @Brian's answer, we have done exactly this. Keep in mind this is Jquery 1.4.2

这是破了一点,可以简化,但它使用的回调,而不是完整的帖子。它最初用于过滤结果实时和重装它们的第一页放回相同的面积。

This is broken out a bit and could be simplified, but it is using callbacks instead of full on posts. It was initially used for filtering results real time and reloading the first page of them back into the same area.

首先,我们有按钮/链接/无论在页面上。这将调出JavaScript来构建JSON数据。

First, we have the button/link/whatever on the page. This is going to call out javascript to build the json data.

<input type="button" alt="Update" onclick="doStuff();" />

然后,我们有doStuff()函数。在这种情况下,精炼是JSON对象的集合

Then, we have the doStuff() function. In this case, refinements is a collection of json objects.

function doStuff() {

        var refinements = GetRefinementSelections();
        var profileId = '<%= Model.Profile.ProfileId %>';

        var startDate = $('#SearchbyDateFrom').val();
        var endDate = $('#SearchbyDateTo').val();

        var jsonData = JSON.stringify(
            { 
                "ProfileId" : profileId,
                "RefinementGroups": refinements,
                "StartDate": startDate,
                "EndDate": endDate
            });

            $('#jsonData').val(jsonData);
            $('#update-button').click();
    }

接下来,我们与doStuff内的隐藏字段一个ajax形式()把我们的数据之中。这可能是一个普通的旧形式以及

Next, we have an ajax form with a hidden field inside that doStuff() puts our data into. This could be a regular old form as well.

<% using (Ajax.BeginForm("MyAction", "MyController", new { },
       new AjaxOptions { },
       new { id = "filteredResultsForm" }))
   {  %>
    <input type="submit" id="update-button" style="display:none;" />
    <%= Html.Hidden("jsonData")%>
<% } %>

因此​​,点击调用此按钮,这将导致回调到服务器。下面是我们的控制器动作。 JsonSerializer是 Newtonsoft.Json

public ActionResult MyAction(string jsonData)
    {
        JsonSerializer serializer = new JsonSerializer();

        StringReader sr = new StringReader(jsonData);
        Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);

        JsonRequest jsonRequest = (JsonRequest)serializer.Deserialize(reader, typeof(JsonRequest));

        //do work with object

        return View();
    }

JsonRequest需要一个类对象,和所有的属性,子类和它们的属性,等等,等等需要是可序列化这种方法的工作。尽管具有流体集要返回的数据(如JSON对象的集合),并没有实际看到的数据的样子,我相当肯定,你可以设计出某种阶级结构来支持你的数据。

JsonRequest needs to be a class object, and all properties, sub classes and their properties, and so on and so forth need to be serializable for this approach to work. Despite having a fluid set of data being returned (as a collection of json objects), and without actually seeing what that data looks like, I am fairly certain you could devise some sort of class structure to support your data.

这篇关于POST的Json没有模型和Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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