简体   繁体   English

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

[英]500 (Internal Server Error) ASP.Net MVC 5 c#

I keep getting the error 500 (Internal Server Error) when I'm trying to save an object. 尝试保存对象时,我不断收到错误500(内部服务器错误)。 I can't seem to find any way to even check what the exact error might be. 我似乎找不到任何方法甚至可以检查确切的错误。

I'm trying to save an object through an API controller and then return the object when complete. 我正在尝试通过API控制器保存对象,然后在完成时返回该对象。 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. 我可以返回一个字符串,错误就会消失,我敢肯定,我可以找到一种方法使它对我有用,但这似乎是解决一个简单问题的肮脏方法。

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! 谢谢!

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. 在我的.edmx中,我默认将实体之间的关联命名为“产品”和“类别”。 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. 这对webapi不利,因此会引发500错误。

Solution: Name your observableArray literally ANYTHING other than the same name as the association. 解决方案:使用与关联相同的名称,以其他任何方式命名您的observableArray。

Thanks for the help! 谢谢您的帮助!

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

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