我不知道为什么我得到的序列中没有元素我有点新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>
答案 0 :(得分:2)
在收集中,您需要了解差异的方法很少
答案 1 :(得分:1)
这是因为你正在使用First()。如果没有具有匹配标题的播音员,则抛出异常。
请参阅https://msdn.microsoft.com/library/bb291976%28v=vs.100%29.aspx