我有3个文本框,第一个是数量,第二个是价格,第三个是总价格。如何在Asp.net MVC中将两个文本框的值相乘
<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("");
}
现在我想乘第一和第二文本框的值,并将其在第三文本框。例如,如果用户在第1个文本框中输入5,在第2个文本框中输入100,则它会在第3个文本框中自动显示500,如果用户更改第1个或第2个文本框的值,则第3个文本框的值也会相应更改。 谢谢。
将文本从前两个框转换为整数,然后将两个文本相乘,将该结果转换为字符串并将其放置在第三个文本框中。 –
谢谢,但我怎样才能得到前两个文本框的值? 我们没有选项可以通过MVC中的textbox.Text获取值,还有其他解决方案吗? – diamond421
看一看[BeginForm(https://msdn.microsoft.com/en-us/library/system.web.mvc.html.formextensions.beginform(V = vs.118)的.aspx)来发表您的模型到控制器。 – GuyVdN