而不是回發的User
所有化子性質,你可以在BeginForm()
方法添加路由值回發的ID或用戶. Assuming that property is named
UserId`,然後
@foreach (var c in Model)
{
<tr>
....
<td>
@using (Html.BeginForm("DisableUser", "UserManagement", new { id = c.UserId))
{
<input type="submit" value="Disable" class="btn btn-primary"/>
}
</td>
</tr>
}
和控制方法是
public ActionResult DisableUser(int id)
{
// Get the User based on id, update it and redirect
}
你也可以考慮使用AJAX提交值,這將允許用戶停留在同一頁面上,並繼續「禁用」等User
的對象,而不需要使一個重定向,在這種情況下的代碼可能是
@foreach (var c in Model)
{
<tr>
....
<td>
<button type="button" class="disable" data-id="@c.UserId">Disable</button>
</td>
</tr>
}
var url = '@Url.Action("DisableUser", "UserManagement")';
$('.disable').click(function() {
var row = $(this).closest('tr');
$.post(url, { id: $(this).data('id') }, function(result) {
if(result) {
// for example, remove the row from the table
row.remove();
} else {
// Oops
}
}).fail(function (result) {
// Oops
});
});
和控制器的方法將是
public JsonResult DisableUser(int id)
{
// Get the User based on id and update it
return Json(true);
// or if the update failed - return Json(null);
}
不要發佈'User',而是發佈'User'的ID,並使用@using(Html.BeginForm(「DisableUser」,「UserManagement」,new {id = User.ID}))'添加路由值並使用'public ActionResult DisableUser(int ID)' –
謝謝@StephenMuecke您的評論非常有幫助。你能否把它作爲答案發布,以便我可以接受它? –