我创建了一个简单的 MVC 应用程序,它从表单中获取信息并将其传递给控制器
看法:
@model MvcApplication1.Models.BetChargeModel
@using (Html.BeginForm())
{
<div>
@Html.TextBoxFor(m=>m.numerators[0]) / @Html.TextBoxFor(m=>m.denominators[0])
</div>
<div>
@Html.TextBoxFor(m => m.numerators[1]) / @Html.TextBoxFor(m => m.denominators[1])
</div>
<div>
<input type="submit" value="Calculate" />
</div>
}
控制器:
public ActionResult Index()
{
BetChargeModel model = new BetChargeModel();
model.numerators = new List<double>();
model.denominators = new List<double>();
model.denominators.Add(1);
model.denominators.Add(1);
model.numerators.Add(0);
model.numerators.Add(0);
return View(model);
}
[HttpPost]
public ActionResult Index(BetChargeModel model)
{
double odds1 = model.numerators[0] / model.denominators[0];
double odds = model.numerators[1] / model.denominators[1];
//other code
}
模型:
public class BetChargeModel
{
public List<double> numerators { get; set; }
public List<double> denominators { get; set; }
public double result { get; set; }
}
当我运行它并尝试从视图中发回信息时,模型返回为空(充满空字段和零)。为什么我的文本框中的数据没有绑定到模型?
(编辑:我已经更改了模型属性和对分子、分母和结果的引用,但为了简洁起见,这里没有更新这些)