Using MVC Object in JavaScript Function

I wish to create a text box as part of an inventory order form that totals the order and displays the sum dynamically upon entry in any of my input fields.

My HTML is as follows:

<label for=”product-quantity”>

@Html.TextBoxFor(c => c.ModelName, new { type = “number”, placeholder = “#”, @class = “pq”, name = “txtbox1”, id = “txtbox1” })


My OrderForm model is as follows:

public int ModelName { get; set; }

I want to take the input value from a box, then multiply it by a number. Adding the total of (ModelName * 100) to the final textbox that will display the value of each of my text boxes.

Note: Some of my text boxes will be (ModelName * 100), some will be (ModelName * 50), others (ModelName * 37), etc…

I’ve tried using JQuery as follows:

<script type="text/javascript"> $(document).ready(function () { $('.textToCalculate').change(function() { var total = 0; $('.textToCalculate').each(function(){ total += parseInt(this.value); }); $('#labelToShowResult').val(total); }); }); </script> 

This works, but it only displays the value that was entered into my textbox, while each box needs to be multiplied as mentioned above before being added to the total sum in my final text box.

submitted by /u/RandomMiscAnon
[link] [comments]

Leave a Reply