SeventhWarhawk
SeventhWarhawk

Reputation: 1323

How could i add an on click event to this checkbox? (HTML Helpers)

I have a checkbox as follows:

@using (Html.BeginForm("ItemsEdit", "Items", FormMethod.Post))
       {

            @Html.CheckBox("itemCheck")

       }

I want to be able to send an additional value to a controller when it is clicked, similar to something like this:

<input id = "@dataLineItem.Split(Model.ItemList.delimiterChar)[0]" type="checkbox" value="true" name="Remember" onclick="sendCheckId(this.id)" />

Below is the jQuery i am using to pass this id value to my controller:

function sendCheckId(checkedId) {
        var sentId = checkedId;
        $.post("/Items/ItemsEdit/", { sendIdToAction: sentId })
    }

However, if i were to use the method above, i wouldnt know how to pass the 'true/false' values of the checkbox, to the same controller.

Upvotes: 0

Views: 231

Answers (2)

SeventhWarhawk
SeventhWarhawk

Reputation: 1323

I found out that i could simply do this using the Razor engine:

@Html.CheckBox("itemCheck", new
{
     onclick = "alert('Hello')"
}

Credit: https://www.c-sharpcorner.com/forums/how-to-call-on-click-event-in-mvc-5

(The answer by Ramesh Palanivel)

Upvotes: 1

Ryan Deckard
Ryan Deckard

Reputation: 78

Maybe try onclick="sendCheckId(this.id,this.value)"

I'm not sure what that sendCheckId function leads to, but it should be pretty quick and easy to expand it to include the second value.

Upvotes: 1

Related Questions