将模型数据从 Knockout 发送回 MVC 3

发布于 2024-12-25 21:04:12 字数 2303 浏览 0 评论 0原文

我是淘汰赛的新手,很难尝试将我的数据从淘汰赛传回我的服务器。我不断收到“没有为此对象定义无参数构造函数”的错误。任何帮助将不胜感激。

我的 Knockout 模型如下

        function partSummary(item) {
            var self = this;
            self.ID = ko.observable(item.ID);
            self.serialNumber = ko.observable(item.SerialNumber);
            self.description = ko.observable(item.Description);
            self.manufacturer = ko.observable(item.Manufacturer);
            self.creationDate = ko.observable(item.DateCreated);
            self.active = ko.observable(item.IsActive);
        }

我将数据发送回服务器的代码是

self.savePart = function() {
                $.ajax("/PartKO/UpdatePart", {
                    data: ko.toJSON(self.partDetails),
                    type: "post",
                    contentType: 'application/json',
                    dataType: 'json'
                });
            };

我的 MVC 控制器是

[HttpPost]
    public JsonResult UpdatePart(PartDetail part)
    {
        var dbPart = new PartGenericAccessor();
        dbPart.ID = part.ID;
        dbPart.Load();
        dbPart.Description = part.Description;
        dbPart.IsActive = Convert.ToBoolean(part.IsActive);

        var manufacturers = ManufacturerAccessor.LoadAll<ManufacturerAccessor>();
        if (part.Manufacturer != null)
        {
            var manufacturer = (from p in manufacturers where p.Name == part.Manufacturer select p.ID).First();
            dbPart.ManufacturerID = manufacturer;
        }

        dbPart.Save();

        return Json("Success!!");
    }

我的服务器端模型是

    public class PartDetail
{
    public PartDetail(Guid id, string serial, string description, string manufacturer, DateTime created, bool isActive)
    {
        ID = id;
        SerialNumber = serial;
        Description = description;
        Manufacturer = manufacturer;
        DateCreated = created.ToShortDateString();
        IsActive = isActive.ToString(CultureInfo.InvariantCulture);

    }

    public Guid ID { get; set; }

    public string SerialNumber { get; set; }

    public string Description { get; set; }

    public string Manufacturer { get; set; }

    public string DateCreated { get; set; }

    public string IsActive { get; set; }
}

I am new to knockout and am having a hard time trying to get my data from Knockout back to my server. I keep getting an error of 'No parameterless constructor defined for this object.' Any help would be appreciated.

My Knockout model is as follows

        function partSummary(item) {
            var self = this;
            self.ID = ko.observable(item.ID);
            self.serialNumber = ko.observable(item.SerialNumber);
            self.description = ko.observable(item.Description);
            self.manufacturer = ko.observable(item.Manufacturer);
            self.creationDate = ko.observable(item.DateCreated);
            self.active = ko.observable(item.IsActive);
        }

My code to send the data back the server is

self.savePart = function() {
                $.ajax("/PartKO/UpdatePart", {
                    data: ko.toJSON(self.partDetails),
                    type: "post",
                    contentType: 'application/json',
                    dataType: 'json'
                });
            };

My MVC controller is

[HttpPost]
    public JsonResult UpdatePart(PartDetail part)
    {
        var dbPart = new PartGenericAccessor();
        dbPart.ID = part.ID;
        dbPart.Load();
        dbPart.Description = part.Description;
        dbPart.IsActive = Convert.ToBoolean(part.IsActive);

        var manufacturers = ManufacturerAccessor.LoadAll<ManufacturerAccessor>();
        if (part.Manufacturer != null)
        {
            var manufacturer = (from p in manufacturers where p.Name == part.Manufacturer select p.ID).First();
            dbPart.ManufacturerID = manufacturer;
        }

        dbPart.Save();

        return Json("Success!!");
    }

And my server side model is

    public class PartDetail
{
    public PartDetail(Guid id, string serial, string description, string manufacturer, DateTime created, bool isActive)
    {
        ID = id;
        SerialNumber = serial;
        Description = description;
        Manufacturer = manufacturer;
        DateCreated = created.ToShortDateString();
        IsActive = isActive.ToString(CultureInfo.InvariantCulture);

    }

    public Guid ID { get; set; }

    public string SerialNumber { get; set; }

    public string Description { get; set; }

    public string Manufacturer { get; set; }

    public string DateCreated { get; set; }

    public string IsActive { get; set; }
}

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

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

发布评论

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

评论(2

忆依然 2025-01-01 21:04:12

您需要为 MVC 模型提供无参数构造函数:

public class PartDetail
{
    public PartDetail()
    { ...  }
}

当数据从服务器返回时,MVC 将使用无参数构造函数创建一个空对象,然后调用“set”方法来设置与传入数据匹配的每个属性。

You need to supply a parameterless constructor for your MVC model:

public class PartDetail
{
    public PartDetail()
    { ...  }
}

When the data comes back from the server, MVC will create an empty object, using the parameterless constructor and then call the 'set' methods to set each property that matches the data coming in.

清秋悲枫 2025-01-01 21:04:12

有一次我犯了愚蠢的错误:

将控制器参数命名为“action” - 并且在发布时总是null

[HttpPost]
public JsonResult AddMetaAction(ActionModel action)

我不知道这一点,并花了大约半小时来调试和解决该问题:(

Once I made stupid mistake:

Named controller argument 'action' - and on post it allways null.

[HttpPost]
public JsonResult AddMetaAction(ActionModel action)

I didn't know about that and spend on debug and solving that problem about a half of hour:(

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