2017-07-10 153 views
-1

我試圖創建自己的複選框,並單擊它時,它會發送一個true或false值回基於結果的控制器將添加用戶id或刪除它,基於此返回結果。我沒有使用模型,我只想在點擊提交請求之後就沒有按鈕。ASP.Net MVC6複選框值控制器

到目前爲止,我有:

@using (Html.BeginForm("Index", "Home", FormMethod.Post)) 
{ 
    <label>View All Tickets @Html.CheckBox("chkAllTickets")</label> 
} 

我只是想發回類似 公衆的ActionResult指數(布爾選中),並檢查點擊時要麼持有真或假。

我需要使用jQuery還是簡單那麼,我只是失去了一些東西。到目前爲止,我已經做好了一切,這給我帶來了很多麻煩。

在此先感謝。

+0

的可能的複製[複選框的值傳遞到控制器在asp.net mvc4動作](https://stackoverflow.com/questions/18862712/pass-values-of-checkbox-to-controller-action- in-asp-net-mvc4) –

+0

你有模型中的這個屬性嗎? –

+0

它是一個類似的問題,但我也想提交,當我點擊chekcbox。不,我寧願沒有在我的模型中,但我在我的控制器作爲公共布爾ChkAllTickets {get;組; } –

回答

0

我解決了複選框的問題。

<div style="display:inline-block"> 
    <label style="margin-left:10px;"> 
     <span><label>View All Tickets @Html.CheckBox("chkAllTickets")</label></span> 
    </label> 
</div> 

我用javascript和jquery來解決這個問題。 如果它對1行動是真的,那麼另一個行爲是假的。

$(document).ready(function() { 
     $('#chkAllTickets').on('change', function() { 
      var theUrl = ''; 
      this.value = this.checked ? 1 : 0; 
      if (this.value == 1) 
      { 
       theUrl = '@Html.Raw(Url.Action("Index", "Home", new { SortField = ViewBag.SortingPagingInfo.SortField, SortDirection = ViewBag.SortingPagingInfo.SortDirection, ChkAllTickets = true }))'; 
       window.location = theUrl; 
      } 
      else 
      { 
       theUrl = '@Html.Raw(Url.Action("Index", "Home", new { SortField = ViewBag.SortingPagingInfo.SortField, SortDirection = ViewBag.SortingPagingInfo.SortDirection, ChkAllTickets = false }))'; 
       window.location = theUrl; 
      } 

     }); 
    }); 
0

亞當嗨,如果我明白你的問題正確,這是很簡單的,那就是,你可以使用該控制器的操作方法的複選框的名稱,並獲得真正的或基於選中或取消選中假。

例如:

@using (Html.BeginForm("Index", "Home", FormMethod.Post)) 
    { 
     <label>View All Tickets @Html.CheckBox("chkAllTickets")</label> 
    } 

在控制器:

Public class HomeController:Controller 
{ 
    public actionresult Index(bool chkAllTickets) 
    { 
      //other logic 
    } 

} 

注:基於綁定的複選框的值,指定動作方法的數據類型。