Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value from submit button to mvc dynamically?

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

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

Model is of type IEnumerable of Employee Model

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

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.

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.

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?

like image 306
Nithin B Avatar asked Dec 15 '22 03:12

Nithin B


1 Answers

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.

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

like image 90
Shyju Avatar answered Dec 28 '22 06:12

Shyju