从列表中删除项目并在模型中获取错误的ajax方法

问题描述 投票:0回答:1

这是我的见识****,我在这里列出我的模特

@model  WebApplication2.Models.NewsListQueryModel
<table class="table table-bordered" id="tblNews" >
                        <thead>
                            <tr>
                                <th>Id News</th>
                                <th>News Time</th>
                                <th>News Name</th>
                                <th>News Author</th>
                                <th>News Content</th>                                
                                <th>Değiştir</th>
                                <th>Sil</th>

                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.NewsList)//this part collapse
                            {
                            <tr>
                                <td>@item.IdNews</td>
                                <td>@item.NewsTime</td>
                                <td>@item.NewsName</td>
                                <td>@item.NewsAuthor</td>
                                <td>@item.NewsContent</td>


                                <td><a class="btn btn-primary" href="/Author/AuthorList/@item.IdNews">Değiştir</a></td>
                                <td><a class="btn btn-warning buttonsil " data-id="@item.IdNews" >Sil</a></td>

                            </tr>
                            }
                        </tbody>
                    </table>

这是我的Ajax代码ı使用此Ajax代码删除项目

 $("#tblNews").on("click", ".buttonsil", function () {
        var btn = $(this);
        bootbox.confirm("Silmek istediğinize eminmisiniz", function (result) {
            if (result) {
                var id = btn.data("id");
                $.ajax({
                    url: "/NewsAdd/Delete/" + id,
                    type: "GET",
                    success: function () {
                        btn.parent().parent().remove();

                    },
                });
            }
        });     
    });

这是我的控制器新闻列表,我在这里列出我的模型删除并删除了带有ajax鳕鱼的项目ID,并在此处删除

 public ActionResult NewsList()
        {
            var mod = new NewsListQueryModel();
            mod.NewsList = lzm.News.ToList();

            return View("NewsList", mod);

        }


        public ActionResult Delete(int id)
        {
            var newsdelete = lzm.News.Find(id);
            if (newsdelete == null)
            {
                return HttpNotFound();
            }
            lzm.News.Remove(newsdelete);
            lzm.SaveChanges();

            return View("NewsList");

        }

这是我的模特

public class NewsListQueryModel
    {
        public List<News> NewsList { get; set; }
    }

当我从列表中删除wiew时收到错误。错误是System.Web.Mvc.WebViewPage.Model.get返回null .System.NullReferenceException:

ajax asp.net-mvc model-view-controller asp.net-ajax nullreferenceexception
1个回答
0
投票

在删除操作中,您返回(“ NewsList”);没有模型,因此在视图中模型将为null,并且foreach中的模型将出错。你可以尝试

@if(Model.NewsList != null && Model.NewsList.Count > 0)
{

}
© www.soinside.com 2019 - 2024. All rights reserved.