简体   繁体   中英

Sequence contains no elements on mvc

I am not sure why am i getting sequence contains no elements im kinda new mvc

My Controller

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

My View

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

In collection there is few methods which you need to understand difference

  1. First - means that if nothing is returned it will throw exception, but it allows collection to have more than first
  2. Single - means get only one item, so if collection match more than one or non you will get exception, this is kind "constraint"
  3. FirstOrDefault - means that collection can have more than one or none items matching
  4. SingleOrDefault - collection should have only one or non items matching items

It's because you're using First(). This throws the exception if there's no Announcer with matching title.

See https://msdn.microsoft.com/library/bb291976%28v=vs.100%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM