发布简单的JSON与jQuery的MVC动作控制器方法 [英] Posting simple json to MVC action controller method with jquery

查看:114
本文介绍了发布简单的JSON与jQuery的MVC动作控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想传递一个简单的对象到MVC控制器的方法,而无需创建一个视图模型吧。在控制器方法的参数始终是空尽管它的ID获取调用。我的jQuery的岗位如下。

I'm just trying to pass a simple object to an MVC controller method without having to create a view model for it. The parameter in the controller method is always null though it id getting called. My jquery post is below.

 $(function () {
    $("button[id$='addidentifier']").click(function (evt) {
        alert('button click');
        evt.preventDefault();
        var postdata = {
            identifier: $('#Identifier').val(),
            goldkey: $('#Goldkey').val()
        };
        $.ajax({
            type: 'POST',
            url: 'altbloomberg/AddIdentifier',
            data: JSON.stringify({ postdata :  postdata }) ,
            success: function(data) {
                 alert('data: ' + data);
            },
            failure: function(a,b,c) {
                alert(a);
            },
            //contentType: "application/json",
            dataType: 'json'//,
            //processData: false
        });
    });

在控制器方法是很简单的,我甚至不具备它的身体做了,因为我想获得数据传递第一。

the controller method is simple enough, I don't even have the body of it done cause I wanted to get the data passing in first.

    [System.Web.Http.HttpPost]
    public ActionResult AddIdentifier(string postdata)
    {
           // put here to place a breakpoint to look at postdata
           string lookhere = "";
    }

我看着其他职位,这是他们似乎,表示要做。这似乎是它应该是很容易。所以,我在做什么错在这里?

I've looked at other post and this is what they "seem" to indicate to do. This seems like it should be easy enough. So what am I doing wrong here?

每下面的一些建议:我改变了jQuery函数传递

Per some suggestions below: I changed the jquery function to pass

         $.ajax({
            type: 'POST',
            url: 'altbloomberg/AddIdentifier',
            contentType: "application/json",
            dataType: 'json',
            data: JSON.stringify(postdata),
            success: function(data) {
                 alert('data: ' + data);
            },
            failure: function(a,b,c) {
                alert(a);
            }
        });

我只是改变了字符串化方法PARAMS和设置内容类型,再改控制器PARAMS到

I just changed the stringify method params and set the content type, then changed the controller params to

    (string identifier, string goldkey).  

这将作为字符串化方法的结果是

This works as the results of the stringify method are

   "{"identifier":"XS0939678792","goldkey":"LAW"}"

那么,为什么那么如果我只用一个参数的控制器方法

So why is it then if I just use ONE param in the controller method

   (String postdata) 

和使用

   JSON.Stringify({postdata:postdata}) 

其结果是

  "{"postdata":{"identifier":"XS0939678792","goldkey":"GOVT"}}" does it NOT work. 

不应以POSTDATA JSON值时,控制器的方法是否匹配?

Shouldn't the controller method match on the postdata json value?

推荐答案

有关AddIdentifier行动的参数应该是一个与识别和GOLDKEY属性或为每个属性的字符串参数模型。

The parameters for AddIdentifier action should be either a model with the 'identifier' and 'goldkey' properties or a string parameter for each of those properties.

这样的:

[System.Web.Http.HttpPost]
public ActionResult AddIdentifier(Identifier model)

或本

[System.Web.Http.HttpPost]
public ActionResult AddIdentifier(string identifier, string goldkey)

如果你真的想要的字符串化的数据,那么你可以把数据类型:'JSON'出来,这将发送字符串

If you really want the stringified data then you can take dataType:'json' out and it will just send the string.

修改

根据您的意见,似乎你想要的东西更有活力。你可以从字符串更改POSTDATA参数的类型为动态。我不知道这是否会开箱或没有。如果没有,你可以创建数据绑定到一个动态参数自定义的模型绑定。

Based on your comments it seems you want something more dynamic. You could change the type of your postdata parameter from string to dynamic. I'm not sure if this will work out of the box or not. If it doesn't you can create your own custom model binder to bind the data to a dynamic parameter.

HTTP://www.codeproject.com /用品/ 605595 / ASP-NET-MVC-定制模型的活页夹

如果我是你,我会坚持以创建模型或定义的参数,但前两个选项之一。

If I were you I would stick to one of the first two options of creating a model or defining the parameters, though.

这篇关于发布简单的JSON与jQuery的MVC动作控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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