Mustang31
Mustang31

Reputation: 282

Options(session variable??) to get set checkbox value with MVC paging and sorting

In my html.beginform I have this checkbox that I want to persist. I'm not sure how to add it to the below ActionLink...or if it is even possible. I thought of using a session variable. If anyone has an example showing how to set a checkbox value in a session variable that would be awesome...or if there is a way to pass it in the ActionLink...that would be cool too. cheers and thanks in advance

      @Html.CheckBoxFor(model=>model.wccrSelection.SendDemand)

      @Html.ActionLink("Order",
        "Display", new {Model.wccrSelection.WccrId,sortOrder = ViewBag.NameSortParm })

Upvotes: 1

Views: 928

Answers (2)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

You can use jQuery to add additional parameter for passing checkbox state to your action method as shown below:

Eg.:

    @Html.CheckBoxFor(model=>model.wccrSelection.SendDemand, new {@class = "SendDemand"})
    @Html.ActionLink("Order",
            "Display", new {Model.wccrSelection.WccrId,sortOrder = ViewBag.NameSortParm }, new { @class = "DisplayOrder" })

<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script type="text/javascript"> 
        $(function(){
          (".DisplayOrder").on.("click",function(){
          var href = $(this).attr("href");
          if ($(".SendDemand").is(':checked')) {
            href = href + "&CheckBoxState=true";
          } 
          else {
            href = href + "&CheckBoxState=false";
          }
          $(this).attr("href", href);
        });
       });
</script>

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038740

You could use a <form> instead of a link. This way the value of the checkbox will be automatically sent to the controller and you don't need to use any javascript:

@using (Html.BeginForm("Display", null, new { id = Model.wccrSelection.WccrId, sortOrder = ViewBag.NameSortParm }))
{
    @Html.CheckBoxFor(model=>model.wccrSelection.SendDemand)
    <button type="submit">Order</button>
}

Upvotes: 2

Related Questions