缺少&QUOT东西;删除"在MVC(EF 4.1)post方法

编程入门 行业动态 更新时间:2024-10-20 07:55:31
本文介绍了缺少&QUOT东西;删除"在MVC(EF 4.1)post方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面这个例子.. msdn.microsoft/en-us /数据/ gg685489

我正在与删除功能的问题。

[HttpPost]公众的ActionResult删除(INT ID,博客博客){    尝试    {        使用(VAR DB =新BlogDataEntities())        {            //有问题这里..只要下一行中的调试运行            //模式..它去追赶..并返回空值的视图。            db.Entry(博客).STATE = System.Data.EntityState.Deleted;            db.SaveChanges();        }        返回RedirectToAction(「指数」);    }    抓住    {        返回查看();    }}

在我签'博客'的参数没有得到一个需要删除的实际博客模式。所有其他方法做工精细(编辑,删除(获取)。等等。但删除后失败。我缺少的东西吗?在此先感谢您的帮助。

编辑:

查看code

@model DBFirstMVC.Models.Blog@ { ViewBag.Title =删除; }  < H2>删除< / H>< H3>的你确定要删除该< / H3 GT&;<&字段集GT;<传奇>博客< /传说>< D​​IV CLASS =显示标签>标题< / DIV>< D​​IV CLASS =显示字段> @ Model.Title< / DIV>< D​​IV CLASS =显示标签> BloggerName< / DIV>< D​​IV CLASS =显示字段> @ Model.BloggerName< / DIV> < /字段集>@using(Html.BeginForm()){  &所述p为H.;    <输入类型=提交值=删除/> |    @ Html.ActionLink(返回目录,索引)  &所述; / P> }

编辑2:非剃刀code鉴于:

<使用%(Html.BeginForm()){%GT;&所述p为H.;    <输入类型=提交值=删除/> |    <%:Html.ActionLink(返回目录,索引)%GT;&所述; / P><%}%GT;

编辑3:(我试过ASPX)

<使用%(Html.BeginForm()){%GT;&所述p为H.;    &所述;%= Html.DisplayForModel();%GT; //尝试Html.EditorForModel也..    <输入类型=提交值=删除/> |    <%:Html.ActionLink(返回目录,索引)%GT;&所述; / P><%}%GT;

最后编辑(修正方案)

@model DBFirstMVC.Models.Blog@ { ViewBag.Title =删除;}< H2>删除< / H>< H3>的你确定要删除该< / H3 GT&;@using(Html.BeginForm()){ &所述p为H.;<&字段集GT;<传奇>博客< /传说>< D​​IV CLASS =显示标签>标题< / DIV>< D​​IV CLASS =显示字段> @ Model.Title< / DIV>< D​​IV CLASS =显示标签> BloggerName< / DIV>< D​​IV CLASS =显示字段> @ Model.BloggerName< / DIV>  <输入类型=提交值=删除/> |@ Html.ActionLink(返回目录,索引)< /字段集>&所述; / P>}

解决方案

上下文可能没有为你的博客条目,因为它不是附加到上下文。

您可能需要首先检索博客,然后将其标记为使用输入方法删除:

[HttpPost]公众的ActionResult删除(INT ID,博客博客){    尝试    {        使用(VAR DB =新BlogDataEntities())        {            //从数据库中检索的博客            VAR realBlog = db.Blogs.Find(blog.Id);           //没有在这里做的,只是重新定位           如果(realBlog == NULL)                返回RedirectToAction(「指数」);          //因为你有实体只是这样做,而不是:          db.Blogs.Remove(realBlog);          db.SaveChanges();        }        返回RedirectToAction(「指数」);    }    赶上(例外)    {        返回查看();    }}

我真的不使用的实体,你的模型虽然的想法一致。您应该使用视图模型来代替。

修改

既然你现在说博客没有被通过,试试这个:

@model博客@using(Html.BeginForm()){    @ Html.EditorForModel()    <输入类型=提交值=删除/>}

您并没有真正给模型绑定任何它需要构建模型的细​​节。

Following this example .. msdn.microsoft/en-us/data/gg685489

I am running into issues with Delete functionality.

[HttpPost] public ActionResult Delete(int id, Blog blog) { try { using (var db = new BlogDataEntities()) { //Having issue here.. as soon as the next line is run in debug //mode .. it goes to catch.. and returns a view with null values. db.Entry(blog).State = System.Data.EntityState.Deleted; db.SaveChanges(); } return RedirectToAction("Index"); } catch { return View(); } }

In the parameters I checked 'blog' does not get the actual blog model that needs to be deleted. All the other methods work fine (Edit, Delete (get)..etc.. but Delete post fails. Am I missing something? thanks in advance for the help.

EDIT:

view code

@model DBFirstMVC.Models.Blog @{ ViewBag.Title = "Delete"; } <h2>Delete</h2> <h3>Are you sure you want to delete this?</h3> <fieldset> <legend>Blog</legend> <div class="display-label">Title</div> <div class="display-field">@Model.Title</div> <div class="display-label">BloggerName</div> <div class="display-field">@Model.BloggerName</div> </fieldset> @using (Html.BeginForm()) { <p> <input type="submit" value="Delete" /> | @Html.ActionLink("Back to List", "Index") </p> }

EDIT 2: Non Razor Code in view:

<% using (Html.BeginForm()) { %> <p> <input type="submit" value="Delete" /> | <%: Html.ActionLink("Back to List", "Index") %> </p> <% } %>

EDIT 3: (I tried in aspx)

<% using (Html.BeginForm()) { %> <p> <%=Html.DisplayForModel();%> //Tried Html.EditorForModel also.. <input type="submit" value="Delete" /> | <%: Html.ActionLink("Back to List", "Index") %> </p> <% } %>

FINAL EDIT (Corrected Solution)

@model DBFirstMVC.Models.Blog @{ ViewBag.Title = "Delete"; } <h2>Delete</h2> <h3>Are you sure you want to delete this?</h3> @using (Html.BeginForm()) { <p> <fieldset> <legend>Blog</legend> <div class="display-label">Title</div> <div class="display-field">@Model.Title</div> <div class="display-label">BloggerName</div> <div class="display-field">@Model.BloggerName</div> <input type="submit" value="Delete" /> | @Html.ActionLink("Back to List", "Index") </fieldset> </p> }

解决方案

The context probably doesn't have an Entry for your Blog because it isn't attached to the Context.

You probably need to retrieve the Blog first and then mark it as deleted using the Entry method:

[HttpPost] public ActionResult Delete(int id, Blog blog) { try { using (var db = new BlogDataEntities()) { // retrieve the blog from the database var realBlog = db.Blogs.Find(blog.Id); // nothing to do here, just redirect if( realBlog == null ) return RedirectToAction("Index"); // since you have the entity just do this instead: db.Blogs.Remove(realBlog); db.SaveChanges(); } return RedirectToAction("Index"); } catch( Exception ) { return View(); } }

I don't really agree with the idea of using your entities as your models though. You should use View Models instead.

EDIT

Since you now are saying that Blog isn't being passed, try this:

@model Blog @using ( Html.BeginForm() ) { @Html.EditorForModel() <input type="submit" value="Delete" /> }

You weren't actually giving the model binder any of the details it needed to construct your model.

更多推荐

缺少&QUOT东西;删除&QUOT;在MVC(EF 4.1)post方法

本文发布于:2023-11-08 13:55:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:东西   方法   MVC   QUOT   post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!