ajax 方法从列表中删除项目并在模型中获取错误
ajax method to delete the item from the list and get error in the model
这是我的wiew****我在这里列出我的模型
@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();
},
});
}
});
});
这是我的控制器 NewsList 我在这里列出我的模型 Delete item id with ajax cod 已在此处删除并删除
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 返回空值。System.NullReferenceException:
你在删除操作return ("NewsList");没有模型,因此在视图模型中将为空,并且 foreach 中的模型将出错。
你可以试试
@if(Model.NewsList != null && Model.NewsList.Count > 0)
{
}
这是我的wiew****我在这里列出我的模型
@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();
},
});
}
});
});
这是我的控制器 NewsList 我在这里列出我的模型 Delete item id with ajax cod 已在此处删除并删除
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 返回空值。System.NullReferenceException:
你在删除操作return ("NewsList");没有模型,因此在视图模型中将为空,并且 foreach 中的模型将出错。 你可以试试
@if(Model.NewsList != null && Model.NewsList.Count > 0)
{
}