无法使用ExtJS将数据发布到ASP.Net Web API Controller

时间:2014-03-20 07:35:36

标签: asp.net-mvc extjs4 asp.net-web-api

我第一次使用ASP.Net MVC Web API。我有两个函数写在我的控制器中。第一个获取记录列表,第二个用于发布数据。获取记录列表的功能很好,而发布数据的功能总是失败。这是控制器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using MyApp.Entity.Models;
using DHT.Library;
using Newtonsoft.Json.Linq;
using System.Collections;

namespace MyApp.Server.Controllers
{
    [RoutePrefix("api/myspace")]
    public class MySpaceController : BaseAppController
    {
        public MySpaceController()
        {
        }

        [AllowAnonymous]
        [Route("")]
        public List<MyApp.Entity.Models.myspace> Get()
        {
            //logic to return records. Works fine.
        }

        [AllowAnonymous]
        public object Post(JObject data)
        {
            string result = "";

            string name;
            JToken nameToken;
            data.TryGetValue("name", out nameToken);
            name = (string)nameToken;

            string address;
            JToken addressToken;
            data.TryGetValue("address", out addressToken );
            address = (string)addressToken;

            //Logic to write data to database. Doesn't work.
        }
    }
}

这就是我尝试使用ExtJS将数据发布到控制器的方法:

AddNewTab: function (button) {
        var me = this,
            tabs = me.getAppMain().down('userpanel'),
            win = Ext.create('Ext.window.Window', {
                title: 'Add User',
                modal: true,
                height: '20%',
                width: '25%',
                items: [{
                    xtype: 'form',
                    layout: 'form',
                    bodyPadding: '5 5 0',
                    id: 'addUserForm',
                    frame: false,
                    items: [{
                        xtype: 'textfield',
                        fieldLabel: 'User Name',
                        maxLength: 25,
                        enforceMaxLength: true,
                        allowBlank: false,
                        id: 'TextUserName'
                    }],
                    buttons: [{
                        text: 'Add',
                        handler: function () {
                            if (this.up('form').getForm().isValid()) {
                                var _Name = this.up('form').getForm().findField('TextUserName').getValue();

                                Ext.Ajax.request({
                                    url: apiUrl + 'myspace',//The URL of controller
                                    mode: 'POST',
                                    params: {
                                        'name': _Name,
                                        'address': 'Dummy Address'
                                    },
                                    success: function (resp) {
                                        var respObj = Ext.decode(resp.responseText);
                                        if (respObj == 'added') {
                                            Ext.Msg.alert('Success', 'User added.');
                                        }
                                        else {
                                            Ext.Msg.alert('Error', respObj);
                                        }
                                    },
                                    failure: function (resp) {
                                        Ext.Msg.alert('Error', 'There was an error.');
                                    }
                                });
                                this.up('form').up('window').close();
                            }
                            else {
                                Ext.Msg.alert('Error', 'Please enter User Name.');
                            }
                        }
                    }, {
                        text: 'Cancel',
                        handler: function () {
                            this.up('form').up('window').close();
                        }
                    }]
                }]
            });
        win.show();
    }

每当我尝试添加用户时,都会收到“出错”消息。我错过了什么步骤?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

[AllowAnonymous]
public object Post(string name, string address)
{
    // your logic
}

并在打开大括号检查时设置断点,以查看名称和地址的值是否通过。