简体   繁体   English

将复杂的JavaScript对象发送到ASP.Net MVC操作

[英]Send complex JavaScript object to ASP.Net MVC Action

This seems to be quite a common theme and a few people have given some very constructive answers, but I'm still struggling to get my attempt to work. 这似乎是一个很普遍的主题,一些人给出了一些非常有建设性的答案,但是我仍在努力工作。

The problem is much the same as this one for example, except that I'm only trying to send a single complex object instead of an array. 问题是有很多相同的这一个 ,例如,不同的是我只是试图发送一个复杂的对象,而不是一个数组。

My controller looks like this: 我的控制器如下所示:

[AcceptVerbs (HttpVerbs.Put)]
[Authorize]
[JsonFilter(Param="Designer", JsonDataType=typeof(Designer))]
public JsonResult SaveProfile(Designer Profile)
{
    ProfileRepository Repo = new ProfileRepository();

    Designer d = Repo.GetById(Profile.ID);
    d.Comments = Profile.Comments;
    d.DisplayName = Profile.DisplayName;
    d.Email = Profile.Email;
    d.FirstName = Profile.FirstName;
    d.LastName = Profile.LastName;


    Repo.Update(d);

    return Json(Profile);
}

The code for retrieving the page data and posting it looks like this: 检索页面数据并将其发布的代码如下所示:

    $('#save-profile').click(function () {

        var Profile = {};
        var context = $('#profile-data')[0];

        $('span', context).each(function () {
            Profile[this.id] = $(this).text();
        });

        Profile.ID = $('h3', context).attr('id');
        console.log(Profile);

        //var DTO = { 'Profile': Profile };

        $.ajax({
            type: "PUT",
            url: "/Home/SaveProfile",
            data: { 'Profile': Profile },
            success: function (data) {
                console.log(data);
            }
        });
    });

The object is being correctly created and posted to the server (I've tried using POST and PUT, by the way), the server appears to be receiving an object instance, but the properties are - as usual - all null. 正在正确创建对象并将其发布到服务器(顺便说一句,我尝试使用POST和PUT),服务器似乎正在接收对象实例,但属性-通常-全部为null。

What am I missing? 我想念什么? I've tried using the approach (adapted) from the example question linked above, but still don't seem to be getting any closer to the solution. 我尝试使用上面链接的示例问题中的方法(自适应),但似乎仍未接近解决方案。 Any help appreciated. 任何帮助表示赞赏。

As it turns out, there's nothing wrong with the ActionResult method itself and neither is there any issue with the JavaScript object or Ajax post. 事实证明,ActionResult方法本身没有错,JavaScript对象或Ajax发布也没有任何问题。 The problem actually lies in the custom filter that decorates the ActionResult and the parameter being set for its param value. 问题实际上出在装饰ActionResult和为其参数值设置的参数的自定义过滤器上。

The following attribute has a parameter of "Designer" set for the name of the parameter I'm trying to pass in. I've supplied this as both the parameter name and the type. 以下属性为我要传递的参数的名称设置了参数“ Designer”。我已将其同时作为参数名称和类型提供。

[JsonFilter(Param="Designer", JsonDataType=typeof(Designer))]

The correct version should be: 正确的版本应为:

[JsonFilter(Param="Profile", JsonDataType=typeof(Designer))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM