ASP.NET MVC 3 - 我的 JsonValueProvider 在哪里?

发布于 2024-12-02 05:20:14 字数 1993 浏览 0 评论 0原文

我正在尝试将 JSON 发送到 Action 方法:

[HttpPost]
public virtual ActionResult KoUpdateAccount(CostCentreDTOX cc)
{
    if (cc.NameCC == null)
    {
        return Json(new { message = "Im null" });
    }
    else
    {
        string s = cc.NameCC;
        return Json(new { message = s });
    }
}

其中 CostCentreDTOX 定义为:

[Serializable]
public class CostCentreDTOX
{
    public int CostCentreId { get; set; }
    public int IdTransactionType { get; set; }
    public string NameCC { get; set; }
}

Json 是通过执行创建的(我使用 Knockoutjs):

var json = ko.toJSON(this.costCentres()[0]);

这会生成以下字符串(这就是我想要的):

"{"CostCentreId":5,"IdTransactionType":2,"NameCC":"Impuestos"}"

将所有内容发送到的方法服务器是:

this.save = function() { 
    var json = ko.toJSON(this.costCentres()[0]);
    api.postCud({
        url: "/admin/Accounts/KoUpdateAccount/",
        dataType: 'JSON',
        data: json,
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(result) { alert(result.message) }
    });
}

其中 this.costCentre()[0] 是一个定义如下的对象:

function costCentre(CostCentreId, IdTransactionType, NameCC) {

    this.CostCentreId      = ko.observable(CostCentreId); 
    this.IdTransactionType = ko.observable(IdTransactionType); 
    this.NameCC            = ko.observable(NameCC);
}

但是,Action 参数 cc 只是实例化为其默认值,就好像 JsonValueProvider 未注册一样。但我使用的是 ASP.NET MVC 3,所以它应该在那里,对吗?就在那里。

编辑:

我尝试将以下内容添加到 Global.asax 文件中:

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
        }

但是,cc 仍然使用默认值进行实例化。

有什么建议吗?

I am trying to send JSON to an Action method:

[HttpPost]
public virtual ActionResult KoUpdateAccount(CostCentreDTOX cc)
{
    if (cc.NameCC == null)
    {
        return Json(new { message = "Im null" });
    }
    else
    {
        string s = cc.NameCC;
        return Json(new { message = s });
    }
}

Where CostCentreDTOX is defined as:

[Serializable]
public class CostCentreDTOX
{
    public int CostCentreId { get; set; }
    public int IdTransactionType { get; set; }
    public string NameCC { get; set; }
}

The Json is created by doing (I am using Knockoutjs):

var json = ko.toJSON(this.costCentres()[0]);

This produces the following string (which is what I want):

"{"CostCentreId":5,"IdTransactionType":2,"NameCC":"Impuestos"}"

The method that sends everything to the server is:

this.save = function() { 
    var json = ko.toJSON(this.costCentres()[0]);
    api.postCud({
        url: "/admin/Accounts/KoUpdateAccount/",
        dataType: 'JSON',
        data: json,
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(result) { alert(result.message) }
    });
}

Where this.costCentre()[0] is an object defined as follows:

function costCentre(CostCentreId, IdTransactionType, NameCC) {

    this.CostCentreId      = ko.observable(CostCentreId); 
    this.IdTransactionType = ko.observable(IdTransactionType); 
    this.NameCC            = ko.observable(NameCC);
}

However, the Action parameter cc just gets instantiated to its default values, as if the JsonValueProvider wasn't registered. But I am using ASP.NET MVC 3, so it should be there, right? Just there.

EDIT:

I have tried adding the following to the Global.asax file:

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
        }

But still, cc gets instantiated with default values.

any suggestions??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2024-12-09 05:20:14

api.postCud 是否做了一些奇怪的事情,在执行帖子时可能会出现问题?您是否尝试过使用 $.ajax() 只是为了看看它是否有效?

Is the api.postCud is that doing something exotic that could problems when it executes the post? Have you tried with $.ajax() instead just to see it that works?

深者入戏 2024-12-09 05:20:14

正如 @Major Byte 怀疑的那样,他们进行 ajax 调用的方式存在问题。这是通过在 api 上定义的方法 api.postCud 完成的。

该方法只是 $.ajax 的包装(请参阅 Eric Sowell 的 MvcConf2 视频:在 ASP.NET 中使用 jQuery 和 Ajax 的不断发展的实践MVC 应用程序)。问题是它使用 $.extend() 合并了 $.ajax 调用的选项,并且我没有包含 dataType 选项的规定。

因此,MVC 不知道 JSON 已被发布,因此模型绑定不起作用。

这就是我睡2小时后发生的事情......

As @Major Byte suspected, there was an issue with they way the ajax call was being made. This is done via a method, api.postCud, defined on an api.

The method is just a wrapper around $.ajax (see Eric Sowell's MvcConf2 video: Evolving Practices in Using jQuery and Ajax in ASP.NET MVC Applications). The problem being that it merges the options for the $.ajax call using $.extend() and I had included no provision for the dataType option.

So MVC was unaware that JSON was being posted, and therefore the model binding wasn't working.

This is what happens when I get 2 hours sleep...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文