我是Asp.net MVC的新手。 我有一个管理用户的应用程序。我的问题是,当我点击用户行的删除链接时,删除方法会在调用渲染删除方法后自动调用[AcceptVerbs(HttpVerbs.Post)]注释。因此,在我点击删除确认按钮之前删除了具有id的用户,之后发生了错误。
以前一切都很好。所以,我不知道为什么会这样。
我的代码:
// GET: /User/Delete/5
[Authorize]
public ActionResult Delete(int id)
{
return View(GetByUserId(id));
}
// POST: /User/Delete/5
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(Users user)
{
//some process
}
第二个方法在调用第一个方法后自动调用。
观点:
<h2>Delete</h2>
<p class="error"><%= Html.Encode(ViewData["messages"]) %></p>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Fields</legend>
<div class="display-label">UserId</div>
<div class="display-field"><%= Html.Encode(Model.UserId) %></div>
<div class="display-label">UserName</div>
<div class="display-field"><%= Html.Encode(Model.UserName) %></div>
<div class="display-label">FullName</div>
<div class="display-field"><%= Html.Encode(Model.FullName) %></div>
<div class="display-label">Email</div>
<div class="display-field"><%= Html.Encode(Model.Email) %></div>
<div class="display-label">DayOfBirth</div>
<div class="display-field"><%= Html.Encode(String.Format("{0:MM/dd/yyyy}", Model.DayOfBirth))%></div>
<div class="display-label">Phone</div>
<div class="display-field"><%= Html.Encode(Model.Phone) %></div>
<div class="display-label">Active</div>
<div class="display-field"><%= Html.Encode(Model.Active) %></div>
</fieldset>
<% using (Html.BeginForm()) { %>
<p>
<input type="submit" value="Delete" /> |
<%= Html.ActionLink("Back to List", "Index") %>
</p>
<% } %>
答案 0 :(得分:0)
答案 1 :(得分:0)
请在下面找到您的修改代码。
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Delete()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(RegisterModel ob)
{
return RedirectToAction("Index");
}
}
Index.cshtml
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<h1>
@Convert.ToString(ViewBag.Delete)
</h1>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http://asp.net/mvc</a>.
<br />
<br />
@Html.ActionLink("Delete record", "Delete");
<br />
<br />
</p>
delete.cshtml
@model MvcApplication1.Models.RegisterModel
@{
ViewBag.Title = "Delete";
}
<h2>
Delete</h2>
<div>
Are you sure you want to delete this?</div>
@using (Html.BeginForm())
{
<p>
<input type="submit" value="Delete" />
|
</p>
}
以上代码与您的代码相同,并且工作正常。请看看。