ASP.NET MVC Sequence Contains No Elements

My HomeController has the following code:

    public ActionResult Edit(int id)
    {
        var ArticleToEdit = (from m in _db.ArticleSet where m.storyId == id select m).First();
        return View(ArticleToEdit);
    }

    [ValidateInput(false)]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Article ArticleToEdit)
    {

        var originalArticle = (from m in _db.ArticleSet where m.storyId == ArticleToEdit.storyId select m).First();
        if (!ModelState.IsValid)
            return View(originalArticle);

        _db.ApplyPropertyChanges(originalArticle.EntityKey.EntitySetName, ArticleToEdit);
        _db.SaveChanges();
        return RedirectToAction("Index");

    }

And this is the kind of Edit method:

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="headline">Headline</label>
            <%= Html.TextBox("headline") %>
        </p>
        <p>
            <label for="story">Story <span>( HTML Allowed )</span></label>
            <%= Html.TextArea("story") %>
        </p>
        <p>
            <label for="image">Image URL</label>
            <%= Html.TextBox("image") %>
        </p>
        <p>
            <input type="submit" value="Post" />
        </p>
    </fieldset>

<% } %>

When I click the submit button, I get an error: The sequence does not contain elements in this line: var originalArticle = (from m in _db.ArticleSet where m.storyId == ArticleToEdit.storyId select m).First();

What is the problem? How to fix it. Thanks

+1
source share
3 answers

You must be sure that when you edit a record, you need to tell the database which record you want to edit. It is not enough to have an identifier in the request if you have not indicated the model to use this identifier (see My second option), but the easiest way to do this is to add a field to your form.

<% using (Html.BeginForm()) {%>
<%= Html.HiddenFor("storyId") %>
    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="headline">Headline</label>
            <%= Html.TextBox("headline") %>
        </p>
        <p>
            <label for="story">Story <span>( HTML Allowed )</span></label>
            <%= Html.TextArea("story") %>
        </p>
        <p>
            <label for="image">Image URL</label>
            <%= Html.TextBox("image") %>
        </p>
        <p>
            <input type="submit" value="Post" />
        </p>
    </fieldset>

<% } %>

, storyId , Querystring. , ?storyId=[n]

[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Article ArticleToEdit, int storyId)
{

    ArticleToEdit.storyId = storyId;

    if (ModelState.IsValid) {
        _db.ApplyPropertyChanges(originalArticle.EntityKey.EntitySetName, ArticleToEdit);
        _db.SaveChanges();
        return RedirectToAction("Index");

    } else {
        return View(ArticleToEdit);
    }
}

- AutoMapper ArticleToEdit ArticleSet, , . , ArticleToEdit ArticleSet, LINQ to SQL storyId.

+1

, , linq _db.ArticleSet. .

FirstOrDefault(), null . FirstOrDetault() null, .

+3

HTML-. ArticleToEdit, , null, .

HTML. , , . :

<% using (Html.BeginForm()) {%>
    <%= Html.HiddenFor("storyId") %>
    ...

, , Single First. , . Single , .

+3

Source: https://habr.com/ru/post/1783708/


All Articles