简体   繁体   中英

DropDownList in ASP.NEt MVC Edit method

Good day, I'm write asp.net mvc3 project with bundle of SQL Server 2012 & EF, I'm use dropdownlist for create & update entry in database. Everything works fine when I add page, but when I'm try to Edit it's unable to save changes. I was try to take brackpoint and in debugging mode Inner exception told that

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

This is my code:

[Table("Pages", Schema = "myprojectname")]
[Bind(Exclude = "PageId")]
public class Page
{
    public int PageId { get; set; }
    public string PageContent { get; set; }
    public int PageTitleId { get; set; }

    public virtual PageTitle PageTitle { get; set; }
}

[Table("PageTitles", Schema = "myprojectname")]
public class PageTitle
{
    public int PageTitleId { get; set; }
    public string Title { get; set; }

    public List<Page> Pages { get; set; }
}

public DbSet<Page> Pages { get; set; }

public DbSet<PageTitle> PageTitles { get; set; }

public ActionResult PageEdit(int id)
    {
        Page page = context.Pages.Find(id);
        ViewBag.PageTitleId = new SelectList(context.PageTitles, "PageTitleId", "Title", page.PageTitleId);
        return View(page);
    }

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult PageEdit(Page page)
    {
        try
        {
            if (ModelState.IsValid)
            {
                context.Entry(page).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index", "Administrator");
            }
        }
        catch (DataException)
        {
            ModelState.AddModelError("", "Unable to save changes. Please try again");
        }
        ViewBag.PageTitleId = new SelectList(context.PageTitles, "PageTitleId", "Title", page.PageTitleId);

        return View(page);
    }

@model BalticCenter.Web.Models.Entities.SitePages.Page

@{
    ViewBag.Title = "PageEdit";
    Layout = "~/Views/Shared/_AdministratorLayout.cshtml";
}

<h2>PageEdit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Page</legend>

    @Html.HiddenFor(x => x.PageId)

    <div class="editor-label">
        @Html.LabelFor(model => model.PageTitleId, "PageTitle")
    </div>
    <div class="editor-field">
        @Html.DropDownList("PageTitleId", String.Empty)
        @Html.ValidationMessageFor(model => model.PageTitleId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PageContent)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.PageContent)
        @Html.ValidationMessageFor(model => model.PageContent)
    </div>

    <p>
        <input type="submit" value="Edit" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

I don't see my mistake. Any ideas?

You have your class Page decorated with [Bind(Exclude = "PageId")]

This means that when you set EntityState to Modified the Entity have no primeary key (id)

context.Entry(page).State = EntityState.Modified;
context.SaveChanges();

And there fore the database can't save the changes

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