使用 MVC 删除视图删除记录

发布于 2024-12-05 01:34:44 字数 1981 浏览 0 评论 0原文

我想做的是列出所有记录,然后提供删除记录的选项。当用户单击索引页面上的删除链接时,他将被重定向到删除确认页面(由 MVC 框架创建的删除视图),现在我所期望的是,当我单击删除视图上的提交按钮时,它将出现在控制器的删除操作中与需要删除的对象。但问题是我得到一个对象,但所有属性都设置为空。

下面是代码:

//GET
        public ActionResult DeleteUser (long id )
        {

            return View(_repository.GetUserById(id));

        }

        //POST
        [HttpPost]
        public ActionResult DeleteUser (UserDTO user)
        {

            _repository.DeleteUser(user.UserId);
            /
            return RedirectToAction("Index");
        }

我期望单击一个提交,然后调用第二个(HttpPost)方法,并且用户将填充这些值,但这并没有发生..

任何人都可以告诉我我做错了什么???

这是我的删除查看代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RepositoryPatternSample.DTOs.UserDTO>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    DeleteUser
</asp:Content>

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

<h2>DeleteUser</h2>

<h3>Are you sure you want to delete this?</h3>
<fieldset>
    <legend>UserDTO</legend>

    <div class="display-label">UserId</div>
    <div class="display-field"><%: Model.UserId %></div>

    <div class="display-label">Username</div>
    <div class="display-field"><%: Model.Username %></div>

    <div class="display-label">FirstName</div>
    <div class="display-field"><%: Model.FirstName %></div>

    <div class="display-label">LastName</div>
    <div class="display-field"><%: Model.LastName %></div>
</fieldset>
<% using (Html.BeginForm()) { %>
    <p>
        <input type="submit" value="Delete User" /> |
        <%: Html.ActionLink("Back to List", "Index") %>
    </p>
<% } %>

</asp:Content>

What I am trying to do is I am Listing all the record and then provide option to delete the record. When User clicks on Delete Link on Index page he is redirected to Delete Confirmation Page( Delete View created by MVC framework), now what I was expecting was when I click on Submit button on the Delete View it will come to my Delete Action of Controller with the object that needs to be deleted. But the problem is I am getting a object but all the properties are set to null.

Below is the code:

//GET
        public ActionResult DeleteUser (long id )
        {

            return View(_repository.GetUserById(id));

        }

        //POST
        [HttpPost]
        public ActionResult DeleteUser (UserDTO user)
        {

            _repository.DeleteUser(user.UserId);
            /
            return RedirectToAction("Index");
        }

I was expecting that one submit is clicked then Second (HttpPost) method will be called and user will be filled with the values, but that is not happening..

can anyone tell me what wrong am I doing ???

This is my Delete View Code

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RepositoryPatternSample.DTOs.UserDTO>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    DeleteUser
</asp:Content>

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

<h2>DeleteUser</h2>

<h3>Are you sure you want to delete this?</h3>
<fieldset>
    <legend>UserDTO</legend>

    <div class="display-label">UserId</div>
    <div class="display-field"><%: Model.UserId %></div>

    <div class="display-label">Username</div>
    <div class="display-field"><%: Model.Username %></div>

    <div class="display-label">FirstName</div>
    <div class="display-field"><%: Model.FirstName %></div>

    <div class="display-label">LastName</div>
    <div class="display-field"><%: Model.LastName %></div>
</fieldset>
<% using (Html.BeginForm()) { %>
    <p>
        <input type="submit" value="Delete User" /> |
        <%: Html.ActionLink("Back to List", "Index") %>
    </p>
<% } %>

</asp:Content>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2024-12-12 01:34:44

您的属性不符合帖子的格式。这就是您看到模型为空的原因。
就我个人而言,我不会传递所有模型属性,而是仅传递 id。
类似的东西

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

和你的控制器将是

[HttpPost]
public ActionResult DeleteUser (int userId)
{
   _repository.DeleteUser(userId);
   return RedirectToAction("Index");
}

Your properties are out of the form post. That's why you see the model null.
Personally instead of passing all the model properties I would pass only the id.
Something like that

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

and your controller would be

[HttpPost]
public ActionResult DeleteUser (int userId)
{
   _repository.DeleteUser(userId);
   return RedirectToAction("Index");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文