Antatrix Antatrix - 1 month ago 8
jQuery Question

MVC radiobutton didn't call ActionResult

I'm trying to call ActionResult by using JQuery. And doing something wrong, because query starts, but ActionResult in controller - not. Also I need to send value of checked radiobutton in it.

JQuery:

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

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


Controller:

[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));
}
}


View:

<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>

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

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