C# 如何在Asp.net MVC中将两个文本框的值相乘

C# 如何在Asp.net MVC中将两个文本框的值相乘,c#,asp.net,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc 4,我有3个文本框,第一个用于数量,第二个用于价格,第三个用于总价 <div class="form-group"> @Html.LabelFor(model => model.quantity, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFo

我有3个文本框,第一个用于数量,第二个用于价格,第三个用于总价

 <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>
现在我想将第一个和第二个文本框的值相乘,并在第三个文本框中显示它们。例如,如果用户在第一个文本框中输入5,在第二个文本框中输入100,则它会在第三个文本框中自动显示500,如果用户更改第一个或第二个文本框的值,则第三个文本框的值也应相应更改。
谢谢。

您可以在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示例。

您可以使用:

 [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("");
    }

希望能有帮助。我在我的应用程序中使用了它,它做了正确的事情。

将前两个框中的文本转换为整数,然后将两者相乘,将结果转换为字符串,并将其放入第三个文本框。谢谢,但如何获得前两个文本框的值?我们没有通过文本框获取值的选项。文本在MVC中,是否有其他解决方案?请查看如何将模型发布到控制器。
 [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("");
    }