-1

I am trying to create a checkbox on its own and when its clicked it will send a true or false value back to the controller based on the result it will add the userId or remove it and return results based on this. I am not using a model and I would like to not have a button just once its clicked to submit the request.

So far I have:

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

And I just wanna send back something like public ActionResult Index(bool Checked) and checked will either hold true or false when clicked.

Do I need to use jQuery or is it simpler then that and I'm just missing something. Up until now I have done everything with ease and this is giving me a lot of trouble.

Thanks in advance.

4
  • Possible duplicate of Pass values of checkBox to controller action in asp.net mvc4 Commented Jul 10, 2017 at 15:12
  • do you have this property in your model? Commented Jul 10, 2017 at 15:13
  • Its a similiar question but I would also like to submit when I click the chekcbox. No and I would prefer to not have it in my model but I have it in a class in my controller as public bool ChkAllTickets { get; set; } Commented Jul 10, 2017 at 15:29
  • Right now you are using a form which means the form needs to submit (typically the result of clicking a "submit" button) in order to contact the server. From your description it sounds like you want it to happen right when they click the checkbox which means an ajax call would be more appropriate. Commented Jul 10, 2017 at 15:33

2 Answers 2

0

Hi Adam if i have understood your problem correctly this is very simple , that is ,you can use the name of the checkbox in the controller action method and get the true or false based on checked or unchecked.

ex:

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

In a controller:

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

}

Note : Based on the value binded to the checkbox , specify your datatype in action method.

Sign up to request clarification or add additional context in comments.

Comments

0

I solved the problem for the checkbox.

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

I used javascript and jquery to solve this problem. If its true to the 1 action, false do the other one.

$(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;
            }

        });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.