2014-10-02 52 views
0

我在我的視圖中有一列表中有一列複選框。我希望用戶能夠根據需要選擇儘可能多的複選框,然後單擊底部的按鈕以禁用它們(此模型中的用戶可以暫停/取消激活並恢復/激活)。我沒有任何錯誤,但是當我點擊按鈕時沒有發生任何錯誤。我附上了我的代碼。謝謝。複選框通過MVC中的ID取消行(視圖/控制器)5

查看

@using (Html.BeginForm("CheckBoxDeactivate")) 
{ 
<table class="table"> 
<tr> 
    <th> 
    Select Users 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.ID) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.FirstName) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.LastName) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.Email) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.UserName) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.suspended) 
    </th> 

    <th></th> 
</tr> 
@foreach (var item in Model) { 
<tr> 
    <td> 
     <input type= "checkbox" name="manageUsers" value="@item.ID" /> 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.ID) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.FirstName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.LastName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Email) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.UserName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.suspended) 
    </td> 

</tr> 
} 
</table> 
<br /> 
<input type="button" value="Add Roles" onclick="location.href'@Url.Action("CheckBoxDeactivate", "User")'" /> 



控制器

[HttpPost] 
     public ActionResult CheckBoxDeactivate(int[] ids) 
     { 
     tbl_Reg_User OAWUser = new tbl_Reg_User(); 
     foreach (var id in ids) 
     { 
      OAWUser = db.tbl_Reg_User.Single(o => o.ID == id); 
      OAWUser.suspended = true; 
      db.SaveChanges(); 
     } 
ViewBag.Message = "Users deactivated successfully!"; 
     return View(); 
    } 
+0

它應該是'的onclick = 「location.href =」 @網址.Action(「CheckBoxDeactivate」,「User」)'「'但這是非常醜陋的.jQuery是不是問題?在一個相關的不是,你應該適當e更改爲您的模型,然後將其重新發送到瀏覽器,以便可以將這些更改可視化。 – 2014-10-02 07:17:01

+0

jQuery是不是沒有問題,我正在尋找任何解決方案,讓這個工作。我將如何去通過jQuery來實現這個功能? – JAY3 2014-10-03 00:26:48

回答

0
<input type= "checkbox" name="manageUsers" value="@item.ID" /> 

該線路應該是

<input type= "checkbox" name="ids" id="ids" value="@item.ID" /> 

我想你沒有得到控制器中的選定複選框。

查看

@using (Html.BeginForm("CheckBoxDeactivate","Home")) 
{ 
    <table class="table" border="1" style="border:1px solid black;border-collapse:collapse;"> 
     <tr> 
      <th>Select Users 
      </th> 
      <th> 
       Sr.No. 
      </th> 
      <th> 
       FirstName 
      </th> 
      <th> 
       LastName 
      </th> 
      <th> 
       Phone 
      </th> 
      <th> 
       IsActive 
      </th> 
     </tr> 
     @foreach (var item in Model) 
     { 
      <tr> 
       <td> 
        <input type= "checkbox" name="ids" id="ids" value="@item.Sr_No" /> 
       </td> 
       <td> 
        @item.Sr_No 
       </td> 
       <td> 
        @item.FirstName 
       </td> 
       <td> 
        @item.LastName 
       </td> 
       <td> 
        @item.Phone 
       </td> 
       <td> 
        @item.IsActive 
       </td> 
      </tr> 
     } 
</table> 

    <br /> 
    <input type="submit" value="Add Roles" onclick="@Url.Action("CheckBoxDeactivate", "User")" /> 
} 

控制器

[HttpPost] 
public ActionResult CheckBoxDeactivate(int[] ids) 
    { 
     Temp OAWUser = new Temp(); 
     if (ids != null) 
     { 
      foreach (var id in ids) 
      { 

       //Your code goes here 
      } 
      ViewBag.Message = "Users deactivated successfully!"; 
     } 
     return RedirectToAction("Temp"); 
    } 

這段代碼做的工作對我來說

+0

這沒有解決它 – JAY3 2014-10-03 00:26:22

相關問題