gpt4 book ai didi

c# - 在 Entity Framework 中编辑对象并将其保存到 ASP.NET MVC 2.0 中的数据库

转载 作者:太空狗 更新时间:2023-10-29 18:30:07 26 4
gpt4 key购买 nike

所以我知道 EF 实体会跟踪自己的更改并在调用 savechanges 时将它们持久化到数据库中,但是这个场景呢...

我有一个专为编辑博文而设计的页面。它有两种 Action 方法。

    [HttpGet]
public ViewResult EditBlogPost(int Id)
{
//This action method gets the blog post by the id and returns the edit blog post page.
BlogPost blogPost = db.BlogPosts.Where(x => x.Id == Id).FirstOrDefault();
if (blogPost == null)
{
ViewData["message"] = "Blog post not found.";
return View("Result");
}
return View("ManageBlogPost", blogPost);
}

[HttpPost]
public ViewResult EditBlogPost(BlogPost blogPost)
{
//This one is where I'm having issues. When model binding populates blogPost, is it auto-tracking still? For some reason SaveChanges() doesn't seem to persist the updates.
if (!ModelState.IsValid)
return View("ManageBlogPost");
db.AttachTo("BlogPosts", blogPost); //I tried this method, it seemed to be what I wanted, but it didn't help.
db.SaveChanges();
ViewData["message"] = "Blog post edited successfully.";
return View("Result");
}

这是返回的 View :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Master.Master" Inherits="System.Web.Mvc.ViewPage<BlogProject.Models.BlogPost>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<% if (Model != null)
{ %>
<h2>Edit Blog Post</h2>
<% }
else
{ %>
<h2>Add Blog Post</h2>
<% } %>
<% using (Html.BeginForm())
{ %>
<% if (Model != null)
{ %>
<%: Html.HiddenFor(x => x.Id)%> <!-- Is this the way to keep un-editable data hidden from the edit form and have them repopulate on the next model bind? What if someone went to modify their POST using something like Fiddler? Couldn't they theoretically edit these fields before the POST? -->
<%: Html.HiddenFor(x => x.Date) %>
<%: Html.HiddenFor(x => x.Author) %>
<%: Html.HiddenFor(x => x.FriendlyUrl) %>
<% } %>
Title:<br />
<%: Html.TextBoxFor(x => x.Title, new { Style = "Width: 90%;" })%>
<br />
<br />
Summary:<br />
<%: Html.TextAreaFor(x => x.Summary, new { Style = "Width: 90%; Height: 50px;" }) %>
<br />
<br />
Body:<br />
<%: Html.TextAreaFor(x => x.Body, new { Style = "Height: 250px; Width: 90%;" })%>
<br />
<br />
<input type="submit" value="Submit" />
<% } %>

</asp:Content>

我在这里有点困惑。添加博客文章似乎工作正常,但编辑它们是另一回事。

最佳答案

解决方案是不要在编辑操作方法中接收博客文章对象。相反,做一些看起来像这样的事情:

[HttpPost]
public ViewResult EditBlogPost(int postID)
{
var post = db.BlogPosts.Single(p => p.PostID = postID);
TryUpdateModel(post);

if (!ModelState.IsValid)
return View("ManageBlogPost");

db.SaveChanges();

ViewData["message"] = "Blog post edited successfully.";
return View("Result");
}

通过这种方式,对象附加到上下文并且 EF 可以正确跟踪更改。 UpdateModel 方法可以节省时间,它会自动将表单集合中的字段与模型中的字段匹配并为您更新它们。

这是 UpdateModel 的文档: MSDN

关于c# - 在 Entity Framework 中编辑对象并将其保存到 ASP.NET MVC 2.0 中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5022943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com