500(内部服务器错误)ASP.Net MVC 5 c#

时间:2022-11-24 08:31:48

I keep getting the error 500 (Internal Server Error) when I'm trying to save an object. I can't seem to find any way to even check what the exact error might be.

当我尝试保存对象时,我不断收到错误500(内部服务器错误)。我似乎无法找到任何方法甚至检查确切的错误可能是什么。

I'm trying to save an object through an API controller and then return the object when complete. I can return a string and the error goes away, which I'm sure I could figure out a way to make that work for me, but that just seems like a dirty way to fix a simple issue.

我正在尝试通过API控制器保存对象,然后在完成时返回对象。我可以返回一个字符串,错误消失了,我确信我可以找到一种方法让我能够工作,但这似乎是解决一个简单问题的一种肮脏方式。

The object saves just fine, it just keeps throwing that error after it runs through my controller.

该对象保存得很好,它只是在它通过我的控制器运行后继续抛出该错误。

This is my API call:

这是我的API调用:

self.postCategory = function (data) {
return $.ajax({
    url: '/API/Category/',
    type: 'POST',
    dataType: 'json',
    data: ko.toJSON(data),
    contentType: 'application/json; charset=urf-8',
    success: function (data) {
        self.getAlert('sucessfully saved category', 1);
        self.Category(data);
    },
    error: function (error) {
        self.getAlert(error, 4);
    }
});
}

This is the controller to handle the call:

这是处理呼叫的控制器:

public Category Post([FromBody]Category category)
{
using (MyContainer db = new MyContainer())
{
    if (db.Categories.Where(n => n.Id == category.Id).Any())
    {
        db.Categories.Attach(category);
        db.Entry<Category>(category).State = EntityState.Modified;
    }
    else
    {
        db.Categories.Add(category);
    }
    try
    {
        db.SaveChanges();
        return category;
    }
    catch (Exception e)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(e);
        return new Category();
    }
}
}

This seems to only happen to categories with records related to it in a one-to-many relationship. New categories or updates to categories without related records do not have the same issue.

这似乎只发生在一对多关系中与其相关的记录的类别。没有相关记录的类别的新类别或更新不具有相同的问题。

It just doesn't make sense to me anymore.

这对我来说再也没有意义了。

Any help would be greatly appreciated!

任何帮助将不胜感激!

Thank you!

1 个解决方案

#1


0  

The problem was with my knockout binding.

问题在于我的淘汰赛绑定。

This:

function CategoryModel(data) {
    var self = this;

    self.Id = data.Id;
    self.Name = ko.observable(data.Name);

    self.Products = ko.observableArray([]);
}

Should have been this:

应该是这样的:

function CategoryModel(data) {
    var self = this;

    self.Id = data.Id;
    self.Name = ko.observable(data.Name);

    self.ProductModels = ko.observableArray([]);
}

In my .edmx I was naming the associations between my entities as Products and Category by default. Then when I made the call it would send the category with the products, make the update to the category, then return the category with complex objects. Which is bad for webapi, and hence the 500 error.

在我的.edmx中,我默认将我的实体之间的关联命名为Products和Category。然后,当我进行调用时,它将发送带有产品的类别,对类别进行更新,然后返回具有复杂对象的类别。这对webapi不利,因此500错误。

Solution: Name your observableArray literally ANYTHING other than the same name as the association.

解决方案:将observableArray命名为除关联名称之外的任何其他名称。

Thanks for the help!

谢谢您的帮助!

#1


0  

The problem was with my knockout binding.

问题在于我的淘汰赛绑定。

This:

function CategoryModel(data) {
    var self = this;

    self.Id = data.Id;
    self.Name = ko.observable(data.Name);

    self.Products = ko.observableArray([]);
}

Should have been this:

应该是这样的:

function CategoryModel(data) {
    var self = this;

    self.Id = data.Id;
    self.Name = ko.observable(data.Name);

    self.ProductModels = ko.observableArray([]);
}

In my .edmx I was naming the associations between my entities as Products and Category by default. Then when I made the call it would send the category with the products, make the update to the category, then return the category with complex objects. Which is bad for webapi, and hence the 500 error.

在我的.edmx中,我默认将我的实体之间的关联命名为Products和Category。然后,当我进行调用时,它将发送带有产品的类别,对类别进行更新,然后返回具有复杂对象的类别。这对webapi不利,因此500错误。

Solution: Name your observableArray literally ANYTHING other than the same name as the association.

解决方案:将observableArray命名为除关联名称之外的任何其他名称。

Thanks for the help!

谢谢您的帮助!