asp.net-mvc - 如何将值从提交按钮动态传递给 mvc?

标签 asp.net-mvc asp.net-mvc-4

我从几天开始就开始使用 MVC 并且在学习在 MVC 中的 Controller 和 View 之间进行通信的很多方法时遇到了一个问题

我有页面以表格形式显示员工列表。

模型是 IEnumerableEmployee Model 类型

它有三个按钮,它们是 EditCreateDeleteDetails

要求:
我使用了按钮,因此所有按钮都应该是 HTTP Post 请求类型,因为我不希望用户使用 URL 请求直接访问它们。

这是我的 View 代码:

@using (Html.BeginForm())
{
    <p>
        <input type="submit" name="CreateView" value="Create New(Post)" formaction="CreateView" formmethod="post" />
    </p>
    <table class="table">
        <tr>
            -------Headings of table-------
        </tr>
        @foreach (var item in Model)
        {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.EmployeeName)</td>
            <td>@Html.DisplayFor(modelItem => item.EmployeeGender)</td>
            <td>@Html.DisplayFor(modelItem => item.EmployeeCity)</td>
            <td>@Html.DisplayFor(modelItem => item.DepartmentId)</td>
            <td>@Html.DisplayFor(modelItem => item.EmployeeDateOfBirth)</td>
            <td>
                <input type="submit" name="EditView" value="Edit(Post)" formaction="Edit" formmethod="post" /> |
                <input type="submit" name="DetailsView" value="Details(Post)" formaction="Details" formmethod="post" /> |
                <input type="submit" value="Delete(Post)" onclick="return confirm('Are you sure you want to delete record with EmployeeId = @item.EmployeeId')" />
            </td>
        </tr>
        }
    </table>
}

这里删除按钮有效,因为我不需要员工的 id。
但是对于其他操作,例如编辑、删除和查看详细信息,我需要将员工 ID 传递给 Controller ​​。但是如何使用提交按钮将 Id 传递给 Controller ​​。

在获取请求类型中,我曾经像这样传递:
@Html.ActionLink("Details", "Details", new { id = item.EmployeeId })

对于单个提交按钮,我曾经像这样传递数据
@using (Html.BeginForm("Details", "BusinessLayer", FormMethod.Post, new { id = item.EmployeeId }))

任何人都可以告诉我我可以休耕来实现这一目标的方法吗?

最佳答案

您可以有 3 个单独的表单标签,每个按钮一个。只需确保表单中有一个输入字段,用于输入要传递的数据。例如,如果您的操作方法接受带有名为 EmployeeId 的参数的 EmployeeId ,则您应该在输入隐藏字段中使用相同的表单。

@model IEnumerable<Employee>
<table>
@foreach(var item in Model)
{
   <tr>
       <td>@item.EmployeeName</td>
       <td>@item.EmployeeGender</td>
       <td>@item.EmployeeCity</td>
       <td>@item.EmployeeDateOfBirth</td>
       <td>
         @using(Html.BeginForm("Details","YourControllerName"))
         {
           <input type="hidden" name="EmployeeId" value="@item.EmployeeId" />
           <input type="submit" value="Details" />
         }
         @using(Html.BeginForm("Edit","YourControllerName"))
         {
           <input type="hidden" name="EmployeeId" value="@item.EmployeeId" />
           <input type="submit" value="Edit" />
         }
         @using(Html.BeginForm("Delete","YourControllerName"))
         {
           <input type="hidden" name="EmployeeId" value="@item.EmployeeId" />
           <input type="submit" value="Delete" />
         }
       </td>
   </tr>

}

还要记住,嵌套表单是无效的 HTML。所以确保你没有这些。

关于asp.net-mvc - 如何将值从提交按钮动态传递给 mvc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660694/

相关文章:

c# - 使用 MachineKey 的加密不是持久的

c# - Entity Framework 中的 NoLock

asp.net-mvc - ServiceStack DefaultRedirectPath 未触发

c# - MVC4+EntityFramework架构

c# - 在 ASP.NET MVC 中获取 DateTime.UtcNow 有多难?

asp.net - 引用类库

AngularJS、.NET Web API 和在 SPA 中返回 View 时的后端授权

asp.net-mvc - 在 mvc 中启用自动迁移

asp.net-mvc - ASP.NET MVC SiteMap 提供程序——如何在实际菜单中显示 'hide' 单项

asp.net-mvc - 模型所有权检查