繁体   English   中英

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

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

这似乎是一个很普遍的主题,一些人给出了一些非常有建设性的答案,但是我仍在努力工作。

问题是有很多相同的这一个 ,例如,不同的是我只是试图发送一个复杂的对象,而不是一个数组。

我的控制器如下所示:

[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);
}

检索页面数据并将其发布的代码如下所示:

    $('#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);
            }
        });
    });

正在正确创建对象并将其发布到服务器(顺便说一句,我尝试使用POST和PUT),服务器似乎正在接收对象实例,但属性-通常-全部为null。

我想念什么? 我尝试使用上面链接的示例问题中的方法(自适应),但似乎仍未接近解决方案。 任何帮助表示赞赏。

事实证明,ActionResult方法本身没有错,JavaScript对象或Ajax发布也没有任何问题。 问题实际上出在装饰ActionResult和为其参数值设置的参数的自定义过滤器上。

以下属性为我要传递的参数的名称设置了参数“ Designer”。我已将其同时作为参数名称和类型提供。

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

正确的版本应为:

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

暂无
暂无

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

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