我有一个观点
@using (Html.BeginForm("ActionMethodName"))
{
@Html.Label("Amount")
@Html.TextBox("txtAmount")
<input class="button" id="submit" type="submit" value="Submit" />
@ViewBag.Average
}
和控制器
public ActionResult ActionMethodName(FormCollection collection)
{
string input = collection.Get("txtAmount");
int numberOfItems = Convert.ToInt32(string.Join(null, System.Text.RegularExpressions.Regex.Split(input, "[^\\d]")));
//float kwota_rachunku=db.RachunekModel.Select(;
ViewBag.Average = db.RachunekModel.OrderByDescending(m => m.RachunekID) // -< typo fixed here!!
.Take(numberOfItems)
.Average(m => m.Podatek);
return View();
}
简单地说,我希望从用户输入的元素数量中获得平均值并将其发送回视图。 我做错了什么?
答案 0 :(得分:0)
这部分视图代码:
@ViewBag.Average
不在任何地方指定平均值。你应该分配它,例如到一个隐藏的领域:
@ViewBag.Average
@Html.Hidden("Average", ViewBag.Average)
对于未来我建议您使用强类型视图,因为它使您的视图和控制器操作的整个创建更加简单。
答案 1 :(得分:0)
试试这个:
@using (Html.BeginForm("ActionMethodName", "YourControllerName"))
{
@Html.Label("Amount")
@Html.TextBox("txtAmount")
<input class="button" id="submit" type="submit" value="Submit" />
@ViewBag.Average
@Html.Hidden("Average", ViewBag.Average)
}
和控制器
public ActionResult ActionMethodName(FormCollection collection)
{
string input = collection.Get("txtAmount");
int numberOfItems = Convert.ToInt32(string.Join(null, System.Text.RegularExpressions.Regex.Split(input, "[^\\d]")));
//float kwota_rachunku=db.RachunekModel.Select(;
ViewBag.Average = db.RachunekModel.OrderByDescending(m => m.RachunekID) // -< typo fixed here!!
.Take(numberOfItems)
.Average(m => m.Podatek);
return View();
}