ASP.NET MVC 5模型绑定不起作用

时间:2022-11-30 17:52:13

I have a model that I am trying to pass to a controller via jQuery's ajax method

我有一个模型,我试图通过jQuery的ajax方法传递给控制器

Here is the model:

这是模型:

public class Package
{
    public string Name;
    public int? Length;
    public int? Width;
    public int? Height;
    public int Weight;
    public bool IsFragile;
    public string Description;
    public PackageType Type;

    public int? Volume
    {
        get
        {
            if (Height != null && Width != null && Length != null)
            {
                return (int) Height*(int) Width*(int) Length;
            }
            return null;
        }
    }

}

My ajax request is done like this:

我的ajax请求是这样完成的:

$.ajax({
            type: "POST",
            url: "/Package/Save",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(this.package),
            success: function (data) {
                self.success('Thank you. Your package has been saved successfully!');
            },
            error: function () {
                self.error('Ooops, something went terribly wrong when saving this package. Please give us a few minutes to fix it!');
            }
        });

Which sends the following to the server:

将以下内容发送到服务器:

{"Name":"asasdas","Length":"15","Width":"31","Height":"326","Weight":"65","IsFragile":false,"Description":"asdasdasdasd","MyType":0,"Volume":null,"Type":"2"}

My controller receives the request but the paramater "savedPackage" has all properties set to default:

我的控制器收到请求,但参数“savedPackage”将所有属性设置为默认值:

    [HttpPost]
    public JsonResult Save(Package savedPackage)
    {
        return Json(new {test = true});
    }

1 个解决方案

#1


10  

Make the public fields properties instead:

改为使用公共字段属性:

public class Package
{
    public string Name { get; set; }
    public int? Length { get; set; }
    public int? Width { get; set; }
    public int? Height { get; set; }
    public int Weight { get; set; }
    public bool IsFragile { get; set; }
    public string Description { get; set; }
    public PackageType Type { get; set; }

    public int? Volume
    {
        get
        {
            if (Height != null && Width != null && Length != null)
            {
                return (int) Height*(int) Width*(int) Length;
            }
            return null;
        }
    }

}

See also this question

另见这个问题

#1


10  

Make the public fields properties instead:

改为使用公共字段属性:

public class Package
{
    public string Name { get; set; }
    public int? Length { get; set; }
    public int? Width { get; set; }
    public int? Height { get; set; }
    public int Weight { get; set; }
    public bool IsFragile { get; set; }
    public string Description { get; set; }
    public PackageType Type { get; set; }

    public int? Volume
    {
        get
        {
            if (Height != null && Width != null && Length != null)
            {
                return (int) Height*(int) Width*(int) Length;
            }
            return null;
        }
    }

}

See also this question

另见这个问题