1

If the checkbox is checked then add value 3 in the original value and if unchecked, minus 3 and keep the original value.

 <label>Cost of this report: $ @Session["ProductCost"]</label>

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{

                        <div>
                            @Html.CheckBoxFor(m => m.IsTrainers)
                            @Html.LabelFor(m => m.IsTrainers, "Include trainers in first Dam")
                        </div>
                        <div>
                            @Html.CheckBoxFor(m => m.IsSales)
                            @Html.LabelFor(m => m.IsSales, "Include Sales in first Dam")
                        </div>
                        <div>
                            @Html.CheckBoxFor(m => m.IsResearch)
                            @Html.LabelFor(m => m.IsResearch, "Research Style (all foals inc)")
                        </div>
                        <div>
                            @Html.CheckBoxFor(m => m.IsEngagement)
                            @Html.LabelFor(m => m.IsEngagement, "Include Race engagements in first Dam")
                        </div>

 }

The model for this checkbox:

public class ClsHome
    {
        public bool IsTrainers { get; set; }
        public bool IsSales { get; set; }
        public bool IsResearch { get; set; }
        public bool IsEngagement { get; set; }
        public bool IsFamilyRunners { get; set; }

    }

Anyone please guide me how to do this.Thank you in advance.

2
  • You've tagged jQuery and ASP MVC, which exactly do you want to use to solve this? You should also post what attempt you've made to solve this yourself as we're not here to write your code for you. Commented Jan 22, 2018 at 11:06
  • @RoryMcCrossan, Yes I know. I do not have an idea so i posted it otherwise i would have done it. I can use either JQuery and MVC. You please suggest me. which one will be good. Commented Jan 22, 2018 at 11:10

2 Answers 2

1

Try the following approach

<label>Cost of this report: $ <span id="spProductCost">@Session["ProductCost"]</span></label>
<input type="hidden" id="productCost" value="@Session["ProductCost"]" />

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
  <div>
      @Html.CheckBoxFor(m => m.IsTrainers, new { onchange = "changeProductCost(this)" })
      @Html.LabelFor(m => m.IsTrainers, "Include trainers in first Dam")
  </div>
  <div>
      @Html.CheckBoxFor(m => m.IsSales, new { onchange = "changeProductCost(this)" })
      @Html.LabelFor(m => m.IsSales, "Include Sales in first Dam")
   </div>
   <div>
        @Html.CheckBoxFor(m => m.IsResearch, new { onchange = "changeProductCost(this)" })
        @Html.LabelFor(m => m.IsResearch, "Research Style (all foals inc)")
   </div>
   <div>
        @Html.CheckBoxFor(m => m.IsEngagement, new { onchange = "changeProductCost(this)" })
        @Html.LabelFor(m => m.IsEngagement, "Include Race engagements in first Dam")
   </div>
}

and add the below javascript

<script>
    var original = parseInt('@Session["ProductCost"]');
    function changeProductCost(chk){
        if ($(chk).prop("checked")) {
            original = original + 3;
        }
        else {
            original = original - 3;
        }
        $("#productCost").val(original);
        $("#spProductCost").text(original);
    }
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

I am not getting any value into original. var original =' ';
Please check how you assigned session value in Session["ProductCost"].
I got the value into original. But how to add this value into my '@Session["ProductCost"]';. please guide me.
I want to add 3 on each checked from checkbox into original value and subtract the same if unchecked from original value. please help me.
Check the updated answer. I have added a hidden field to hold the product cost. Initially it will get value from session then on each change of checkbox changed value will be updated in hidden field.
|
1

You would need to add a common class to each checkbox and then write a change event in jquery for that and do the calculations like:

@Html.CheckBoxFor(m => m.IsTrainers, new {@class = "Pedigrees"})

<input type="hidden" id="ProductCost" value="@Session["ProductCost"]"/>

and in jquery code you would have to write:

$(".Pedigrees").on("change",function(){
    var $productCost = $("#ProductCost");
    if(this.checked)
         $productCost.val( $productCost.val() + 3);
    else
         $productCost.val( $productCost.val() - 3);
   });

or another approach can be to check the state of the checkboxes checked in the control and add and subtract the values accordingly in the ProductCost.

Note that this is just to give you idea how it can be done, the code might need some tweaks to get it perfectly working in your use case.

Hope it helps.

2 Comments

I am not getting value in ` var $productCost = $("#ProductCost");`. please guide me.
that is a hidden field in view : <input type="hidden" id="ProductCost" value="@Session["ProductCost"]"/>

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.