【发布时间】:2016-01-29 20:00:00
【问题描述】:
我有 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>
这里是控制器:
[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("");
}
现在我想将第一个和第二个文本框的值相乘并在第三个文本框中显示它们。例如,如果用户在第一个文本框中输入 5,在第二个文本框中输入 100,那么它会在第三个文本框中自动显示 500,如果用户更改第一个或第二个文本框的值,那么第三个文本框的值也应该相应更改。 谢谢。
【问题讨论】:
-
将前两个框中的文本转换为整数,然后将两者相乘,将结果转换为字符串并将其放在第三个文本框中。
-
谢谢,但是我怎样才能得到前两个文本框的值呢?我们没有选项可以通过 MVC 中的 textbox.Text 获取值,还有其他解决方案吗?
-
查看BeginForm 将您的模型发布到控制器。
标签: c# asp.net asp.net-mvc-4