开发者

Asp.net mvc delete with AcceptVerbs HttpVerbs.Post method auto invoke problem

I'm new to Asp.net MVC. I have an application that manages users. My problem is that when I click on a delete link of a user row, the delete method has [AcceptVerbs(HttpVerbs.Post)] annotation is called automatically after a render delete method invoked. Thus, the user with id is deleted before I click on the delete confirm button and the error has occurred afterward.

Everything worked fine before. So, I don't know why this happend.

My code:

        // 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
        }

The second method invokes automatically after the first one is called.

The view:

<h2>Delete</h2>
<p class="error"><%= Html.Encode(ViewData["messages"]) %>开发者_如何学Go;</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>
<% } %>


You can create another view which will solve your purpose of confirmation delete.

Please find more details using following link.

http://forums.asp.net/t/1513258.aspx


Please find your modify code below.

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>
}

Above code is same as your code and it's working fine. Please have a look.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜