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?
@Html.BeginForm("Edit", "yourControllerName, new { id = item.EmployeeId })) { <input type="submit" value="Edit" /> }etcnew { id = item.EmployeeId }) And why would you want only one POST method instead of one for each action? (although you can do it by giving each submit button the same name (sayname="action") and adding a parameterstring actionin your single POST method)`