0

this is my wiew****I list my my model here

@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>

this is my ajax code ı delete item using this ajax code

 $("#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();

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

this is my controller NewsList I list my my model here Delete item id with ajax cod is removed and deleted here

 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");

        }

this is my model

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

I get the error when I delete an element from the list in wiew. Error is System.Web.Mvc.WebViewPage.Model.get returned null .System.NullReferenceException:

1 Answers1

0

In delete action you return ("NewsList"); without model, so in the view model will be null, and the model in foreach will get error. you can try

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

}
Nhien
  • 164
  • 1
  • 1
  • 9