2017-08-16 40 views
1

我想通過使用JQuery調用ActionResult。並做錯了什麼,因爲查詢開始,但在控制器的ActionResult - 不是。此外,我需要發送檢查單選按鈕的價值。MVC單選按鈕沒有調用ActionResult

JQuery的:

$("#allRows").on("change", function() { 
     var url = '@Url.Action("Index", "OrderManagment")'; 

     if ($("#allRows").attr("checked") === "checked") { 
      $.post(url, { 'tableRows': $("#allRows").val() }); 
     } 
    }); 

控制器:

 [HttpPost] 
     [ValidateAntiForgeryToken] 
     public IActionResult Index(string tableRows) 
     { 
      //some code 

      if (tableRows == "all") 
      { 
       return View(orders.ToList()); 
      } 
      else 
      { 
       var rows = Int32.Parse(_context.Settings.FirstOrDefault().Value); 
       return View(orders.ToList().Take(rows)); 
      } 
     } 

查看:

<form asp-action="Index"> 
<div style="text-align: right;"> 
     <label> Select count of rows in the table</label> 
     <label><input type="radio" id="allRows" name="tableRows" value="all"> All</label> 
     <label><input type="radio" id="settings" name="tableRows" value="settings" checked/> Settings </label> 
    </div> 
</form> 
+1

與'ID = 「allRows」'你的單選按鈕沒有'檢查'屬性。使用'if($('#allRows')。is(':checked')){...' –

+0

你也不會對你返回的視圖做任何事情。 –

+0

@StephenMuecke,一點點它的作品。也許我以錯誤的方式發佈或發佈價值? ActionResult仍然沒有發生。 – Antatrix

回答

1
$("#allRows").on("change", function() { 
     var url = '@(Url.Action("Index", "OrderManagment"))'; 

     if ($(this).is(":checked")) { 
      $.post(url, { tableRows: $("#allRows:checked").val() , function(data){ 
       alert(data); 
      }); 
     } 
    });