私は数日からMVCで作業を始め、MVCのコントローラとビューの間で通信する方法をたくさん学ぶことから質問を受けました提出ボタンからmvcに値を動的に渡す方法は?
私は従業員のリストを表形式で表示しています。
モデルは、それは彼らがEdit
、Create
、Delete
、Details
である3つのボタンがありEmployee Model
の種類IEnumerable
です。
要件:私は、ユーザーが直接URLリクエストを使用してそれらにアクセスしたくないので、すべてのHTTP POSTリクエストタイプのものでなければならないように、私はボタンを使用
。ここで
は、私の見解コードです:私は、従業員のIDを必要としないので、ここで
@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をコントローラに渡す必要があります。しかし、Submitボタンを使ってIDをコントローラーに渡すにはどうすればいいですか?いずれかが私ができることを私にアプローチを伝えることができ、私はこの
@using (Html.BeginForm("Details", "BusinessLayer", FormMethod.Post, new { id = item.EmployeeId }))
などのデータを渡すために使用される単一の送信ボタンについて
@Html.ActionLink("Details", "Details", new { id = item.EmployeeId })
:GETで
は、私がこのように渡すために使用されるタイプを要求しますこれを達成するために休む?
1つのフォーム内に複数の提出をすることは悪い考えです。代わりにボタンを使用し、javascriptの助けを借りて提出することができます。 –
@PrashantMohite、OPはリダイレクトしたいので、ajaxは動作しません。 –
'@Html.BeginForm(" Edit "、" yourControllerName、new {id = item.EmployeeId})){}'など –