序列不包含 mvc 上的元素
本文关键字:元素 mvc 包含 | 更新日期: 2023-09-27 18:35:17
我不确定为什么我得到的序列不包含任何元素,我有点新 mvc
我的控制器
[HttpPost]
public ActionResult DeleteAnnounce(string announce)
{
using (var db = new HarmonyMainServerDbContext())
{
ANNOUNCEMENT annoude = db.Announcer.First(c => c.AnnounceTitle == announce);
db.Announcer.Remove(annoude);
db.SaveChanges();
}
return RedirectToAction("ViewAnnounce");
}
我的观点
<div id="clientDetailsContainer">
@using (Html.BeginForm("Announcements", "Admin", FormMethod.Post, new { id = "Announcers" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="module-controls" style="display:block;">
<a href="/Admin/ViewAnnounce" class="k-button module-action"><img src="../Images/src/back.png"/><span>Back to List</span></a>
<a class="k-button module-action" id="submitSave" title="haha"><img src="../Images/src/subgrid_save.png"/><span>Save</span></a>
<a class="k-button module-action" id="submitDelete" ><img src="../Images/src/subgrid_save.png"/><span>Delete</span></a>
</div>
<h2>Announcement</h2>
@Html.HiddenFor(model => model.AnnounceID)
<div class="client-details" style="margin-left: 50px">
<p>
<label class="label-ant">Announcement Title</label>
@Html.TextBoxFor(model => model.AnnounceTitle, new {@class="k-textbox", @style="width:250px" })
@Html.ValidationMessageFor(model => model.AnnounceTitle)
</p>
<p>
<label class="label-ant">Announcement Remarks</label>
@Html.TextBoxFor(model => model.AnnounceRemarks, new {@class="k-textbox", @style="width:250px" })
@Html.ValidationMessageFor(model => model.AnnounceRemarks)
</p>
<p>
<label class="label-ant">Announce Link</label>
@Html.TextBoxFor(model => model.AnnounceLink, new {@class="k-textbox", @style="width:250px" })
@Html.ValidationMessageFor(model => model.AnnounceLink)
</p>
</div>
}
<script>
$("#submitSave").click(function () {
$("#Announcers").submit();
});
$("#submitDelete").click(function () {
$("#Announcers").attr({
"action" : "/Admin/DeleteAnnounce",
});
});
</script>
</div>
在集合中,您需要了解差异的方法很少
- 首先 - 意味着如果什么都没有返回,它将抛出异常,但它允许集合不止第一个
- 单个 - 表示只获得一个项目,因此如果集合匹配超过一个或没有你会得到例外,这是一种"约束"
- FirstOrDefault - 表示集合可以有多个项目匹配或没有匹配项
- 单个或默认 - 集合应只有一个或非项目匹配项目
这是因为您使用的是First()。如果没有具有匹配标题的播音员,这将引发异常。
请参阅 https://msdn.microsoft.com/library/bb291976%28v=vs.100%29.aspx