敲除删除500错误

时间:2016-02-16 14:26:46

标签: javascript ajax asp.net-mvc knockout.js

当我尝试使用ajax删除列表中的项目时,收到500错误。

使用Chrome上的控制台结合Fiddler,ID上的空引用就是问题所在。通过Chrome控制台进行调试显示公告对象确实具有Id。

我的印象是来自服务器的500个错误,但我在它正在调用的函数路径上有一个断点而没有被击中。

有没有什么东西让我盯着为什么会这样设置公告。我要归零吗?

控制器:

public JsonResult DeleteAnnouncement(int Id)
        {
            if(repository.DeleteAnnouncement(Id))
            {
                return Json(repository.GetAllAnnouncements(), JsonRequestBehavior.AllowGet);
            }
            return Json(null);
        }

存储库:

public bool DeleteAnnouncement(int Id)
        {
            var model = (from a in db.DbAnnouncement
                                     where a.Id == Id
                                     select a).SingleOrDefault();
            if (model.Id != 0)
            {
                db.DbAnnouncement.Remove(model);
                db.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

js script:

<script type="text/javascript">
        function AnnouncementViewModel() {
            //Make the self as 'this' reference
            var self = this;
            //Declare observable which will be bind with UI
            self.Id = ko.observable("");
            self.Title = ko.observable("");
            self.Details = ko.observable("");
            self.DTCreated = ko.observable("");
            self.LastUpdated = ko.observable("");
            self.CreatedUser = ko.observable("");
            self.UpdatedUser = ko.observable("");
            self.VersionNum = ko.observable("");
            self.StartDT = ko.observable("");
            self.ExpiryDT = ko.observable("");
            self.Enabled = ko.observable("");
            self.GroupID = ko.observable("");
            self.URLLink = ko.observable("");

            var Announcement = {
                Id: self.Id,
                Title: self.Title,
                Details: self.Details,
                DTCreated: self.DTCreated,
                LastUpdated: self.LastUpdated,
                CreatedUser: self.CreatedUser,
                UpdatedUser: self.UpdatedUser,
                VersionNum: self.VersionNum,
                StartDT: self.StartDT,
                ExpiryDT: self.ExpiryDT,
                Enabled: self.Enabled,
                GroupID: self.GroupID,
                URLLink: self.URLLink
            };

            self.Announcement = ko.observable();
            self.Announcements = ko.observableArray(); // list of announcements

            // Delete announcement
            self.delete = function (Announcement) {
                if (confirm('Are you sure to Delete "' + Announcement.Title + '"?')) {
                    $.ajax({
                        url: '@Url.Action("DeleteAnnouncement","Announcement")',
                        cache: false,
                        type: 'POST',
                        contentType: 'application/json; charset=utf-8',
                        //data: ko.toJSON(Announcement.Id),
                        data: {Id: Announcement.Id},
                        success: function (data) {
                            self.Announcements.remove(Announcement);
                            alert("Record Deleted Successfully");
                        }
                    }).fail(
                    function (xhr, textStatus, err) {
                        alert(err);
                    });
                }
            }


        }
        $(document).ready(function () {
            var viewModel = new AnnouncementViewModel();
            ko.applyBindings(viewModel);
        });

1 个答案:

答案 0 :(得分:1)

您正在使用ajax contentType作为Json,但没有真正向服务器发送json对象。使用

data: JSON.stringify({ Id: Announcement.Id() }),

并使用[HttpPost]

装饰您的mvc操作
[HttpPost]
public JsonResult DeleteAnnouncement(int Id)
{}