我需要为移动电脑制作网络应用程序 - > Datalogic Skorio。 在用户输入0之前,它会累计数量。 它将计数器变量加起来的所有内容都重置为0。
这是代码
控制器:
[HttpGet]
public ActionResult Quant()
{
return View();
}
[HttpPost]
public ActionResult Quant(Qnt qnt)
{
int qunt = 0;
string user = (string)TempData.Peek("user");
string pass = (string)TempData.Peek("pass");
motorp.AbreEmpresaTrabalho(Interop.StdBE900.EnumTipoPlataforma.tpProfissional, "DEMO", user, pass); // this part it's for using an external software that contains the products and everything.
while (qnt.qntd != 0 )
{
qunt = qunt+qnt.qntd;
return View();
}
return RedirectToAction("Fin");
}
查看:
@using Trabalho1.Models
@model Qnt
@{
ViewBag.Title = "Quantidade";
}
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewBag.Title </title>
</head>
<div class="container-fluid">
<div>
@using (@Html.BeginForm())
{
<label>
<p>Quantidade desejada:</p>
@Html.TextBoxFor(m => m.qntd, new { @class = "form-control" })
</label>
<p>@Html.Raw(ViewData["qunt"])</p> \\ ignore this line.testing something
<button class="btn btn-info">Add</button>
}
</div>
<div class=" row">
<div class="col-md-2">0)Sair</div>
</div>
</div>
答案 0 :(得分:2)
当用户在View中输入值时,它将转到post方法
[HttpPost]
public ActionResult Quant(Qnt qnt)
{
.....
}
在此方法中,您声明变量int qunt = 0;
同样在while循环中,如果没有,则检查qnt.qntd !=0
然后它将进入循环并进行求和,但下一行是return view();
,因此它将再次返回到视图。然后用户键入值,然后如上所述开始处理循环。
EG: **`Cycle 1`**
User entered 2
Control come to the controller
qunt is set to zero //qunt = 0
checking 2 != 0 // true
Enter the while loop
qunt = qunt + 2 // qunt = 0 + 2 = 2
return view()//go to the view again
**Cycle 2**
user entered 5
Control come to the controller
qunt is set to zero //qunt = 0 --rested 2 to 0 here
checking 5 != 0 // true
Enter the while loop
qunt = qunt + 5 // qunt = 0 + 5 = 5
return view()//go to the view again
**Cycle 3**
user entered 0
Control come to the controller
qunt is set to zero //qunt = 0 --rested 5 to 0 here
checking 0 != 0 // false
wont enter the loop
希望你理解。因此,qunt中的值将为0,并且不需要while循环,因为在第一次循环时它将返回到视图。