2016-05-27 20 views
3

我有哪裏我顯示的用戶列表的視圖的方法,設置模式爲:後一個從模型收集項目控制器MVC

@model List<MyProject.Models.User> 

按照這種觀點我希望能夠選擇執行鍼對特定用戶的操作,即發佈到要禁用用戶的控制器。如何將特定的用戶對象發佈到控制器?

這是我這麼遠,但我看不出如何從收集發佈特定對象:

@foreach (var c in Model) 
{ 
    <tr> 
     <td>@c.Username</td> 
     <td>@c.IsEnabled</td> 
     <td> 
      @using (Html.BeginForm("DisableUser", "UserManagement")) 
      { 
       <input type="submit" value="Disable" class="btn btn-primary"/> 
      } 
     </td> 
    </tr> 
} 

我的控制器具有簽名:

public ActionResult DisableUser(User user) 
+2

不要發佈'User',而是發佈'User'的ID,並使用@using(Html.BeginForm(「DisableUser」,「UserManagement」,new {id = User.ID}))'添加路由值並使用'public ActionResult DisableUser(int ID)' –

+0

謝謝@StephenMuecke您的評論非常有幫助。你能否把它作爲答案發布,以便我可以接受它? –

回答

2

而不是回發的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); 
} 
0

禁用用戶的簡單方法是使用Html.ActionLink而不是表單 - 您應該能夠在模板代碼中看到大量這樣的示例。操作鏈接可以重定向到確認頁面,也可以禁用用戶並重定向到消息頁面,說明「用戶已被禁用」。

我更好的方法是使用AJAX。你可以用jQuery來做到這一點,或者你可以使用MVC Ajax formAjax Action Link。我會建議你的谷歌MVC Ajax行動鏈接的例子。

您可能還需要將鏈接的類別設置爲Bootstrap 'btn' class

-1

首先,您不得在任何循環內聲明Html.BeginForm以將對象發佈到控制器。

@using (Html.BeginForm("DisableUser", "UserManagement")) 
{ 
    @foreach (var c in Model) 
    { 
     <tr> 
      <td>@c.Username</td> 
      <td>@c.IsEnabled</td> 
      <td>   
       <input data-id="@c.Id" type="submit" value="Disable" class="clsBtnPost btn btn-primary"/>   
      </td> 
     </tr> 
    } 
} 

這是供你參考

$(document).ready(function(){ 
    $(".clsBtnPost").click(function(){ 
     var userId = $(this).data("id"); 
     $.ajax({ 
     contentType: 'application/json; charset=utf-8', 
     dataType: 'json', 
     type: 'POST', 
     url: 'ControllName/MethodName', //Your Url 
     data: { 'userId': userId }, 
     success: function() {   
      alert("successfully called."); 
     }, 
     failure: function (response) {   
      alert("Error"); 
     } 
     }); 
    }); 
}); 

您可以用阿賈克斯呼叫轉入一個對象傳遞給控制器​​。

+0

請有人會讓我知道-1什麼? –

+1

你已經展示了一個使用ajax的替代方法,如果OP想要保持在同一頁面上(儘管它不直接回答這個問題),那麼這很好。但是你的陳述_你不能在任何loop_中聲明Html.BeginForm(它完全正確和通用,特別是對於刪除集合中的項目),而且你必須跟Ajax_一起完全錯誤。考慮編輯你的問題,刪除這些陳述,並注意這是一個替代方案(及其好處) –

+0

@StephenMuecke感謝您的指導。 –

0

有張貼單個用戶

@foreach (var c in Model) 
{ 
<tr> 
    <td>@c.Username</td> 
    <td>@c.IsEnabled</td> 
    <td> 
     @using (Html.BeginForm("DisableUser", "UserManagement")) 
     { 
      <input type="text" name="Username" value="@c.Username"> 
      <input type="text" name="IsEnabled" value="@c.IsEnabled"> 
      <input type="hidden" name="id" value="@c.id"> 

      <input type="submit" value="Disable" class="btn btn-primary"/> 
     } 
    </td> 
</tr> 
} 

的想法是一樣的,由於時間不夠,請根據您的要求進行,使用類屬性名稱輸入名稱,該操作將一種方式獲得價值

相關問題