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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39660694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:33:47  来源:igfitidea点击:

How to pass value from submit button to mvc dynamically?

asp.net-mvcasp.net-mvc-4

提问by Nithin B

I started working with MVC from few days and got a question out of learning lot of ways to communicate between Controllers and Views in MVC

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

I have page that shows list of employees in tabular form.

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

Model is of type IEnumerableof Employee Model

模型类型IEnumerableEmployee Model

It has three buttons they are Edit, Create, Delete, Details.

它有三个按钮,分别是EditCreateDeleteDetails

Requirement:
I used buttons so that all should be of HTTP Post request type because I do not want users to directly access them using URL requests.

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

Here is my view code:

这是我的视图代码:

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

Here delete button works because I do not need the id of the employee. But for other actions like editing, deleting and details viewing I need to pass Employees Id to controller. But how do I pass the Id to the controller using submit button.

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

In get requests types I used to pass like this:

在获取请求类型中,我曾经像这样传递:

@Html.ActionLink("Details", "Details", new { id = item.EmployeeId })

For single submit button I used to pass data like this

对于单个提交按钮,我曾经像这样传递数据

@using (Html.BeginForm("Details", "BusinessLayer", FormMethod.Post, new { id = item.EmployeeId }))

Can any one tell me the approach that I can fallow to achieve this?

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

回答by Shyju

You can have 3 separate form tags, one for each button. Just make sure you have an input field inside the form for the data you want to pass. For example if your action method is accepting the EmployeeId with a a parameter called EmployeeId, you should have in input hidden field with the same inside the form.

您可以有 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>

}

Also remember, nested forms are invalid HTML. So make sure you do not have those.

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