How to multiply the values ​​of two text fields in Asp.net MVC

I have 3 text fields, 1st place for quantity, 2nd place for price and 3rd place for total price.

 <div class="form-group">
            @Html.LabelFor(model => model.quantity, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.quantity, new { htmlAttributes = new { @class = "form-control", @type = "number" } })
                @Html.ValidationMessageFor(model => model.quantity, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.price, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.price, new { htmlAttributes = new { @class = "form-control", @type = "number" } })
                @Html.ValidationMessageFor(model => model.price, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.totalprice, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">  
                    @Html.EditorFor(model => model.totalprice, new { htmlAttributes = new { @class = "form-control", @type = "number" } })
                    @Html.ValidationMessageFor(model => model.totalprice, "", new { @class = "text-danger" })             
            </div>

and here is the controller:

        [HttpPost]
        public ActionResult Add(Model model)
        {
            obj.Quantity = model.quantity;
            obj.Price = model.price;
            obj.TotalPrice = model.totalprice

            db.Details.Add(obj);
            db.SaveChanges();
            return RedirectToAction("");
        }

Now I want to multiply the values ​​of the 1st and 2nd text fields and show them in the 3rd text field. For example, if users enter 5 in the 1st text box and 100 in the second text box, then 500 are automatically displayed in the third text box, and if users change the value of the 1st or 2nd text box, the value of the 3rd text box is also should change accordingly. Thanks.

+4
source share
2 answers

keyup javascript, .

, jQuery.

$(function(){

  $("#quantity,#price").keyup(function(e){

    var q=$("#quantity").val();
    var p=$("#price").val();
    var result="";

    if(q!=="" && p!=="" && $.isNumeric(q) && $.isNumeric(p))
    {
      result = parseFloat(q)*parseFloat(p);
    }
    $("#totalPrice").val(result);

  });

});

- jsbin.

+4

:

 [HttpPost]
    public ActionResult Add(Model model)
    {
        obj.Quantity = model.quantity;
        obj.Price = model.price;
        obj.TotalPrice = model.totalprice

        model.totalprice = model.quanity * model.price
        db.Details.Add(obj);
        db.SaveChanges();
        return RedirectToAction("");
    }

, . , .

0

Source: https://habr.com/ru/post/1626577/


All Articles