我的问题:如果我们已经将 view -a 设为强类型,那么为什么我们需要将模型对象从控制器 post 方法返回给 view - MVC 4 asp.net?
例如:我有计算器视图:
@using (Html.BeginForm())
{
<p>Number One : @Html.TextBoxFor(m => m.numberOne)</p>
<p>Number Two : @Html.TextBoxFor(m => m.numberTwo)</p>
<input type="submit" value ="Addition" name="calculateOperation" />
<input type="submit" value ="Subtraction" name="calculateOperation" />
<input type="submit" value ="Multiplication" name="calculateOperation" />
<input type="submit" value ="Division" name="calculateOperation" />
}
@using (Html.BeginForm())
{
<p>Output : @Html.TextBoxFor(m => m.result)</p>
}
和控制器:
public ActionResult Calculate(Calculator model, string calculateOperation)
{
if (calculateOperation.Equals("Addition"))
{
int[] array = { 1, 12, 5, 26, 7, 14, 3, 7, 2 };
model.result = model.numberOne + model.numberTwo;
}
if (calculateOperation.Equals("Subtraction"))
{
model.result = model.numberOne - model.numberTwo;
}
if (calculateOperation.Equals("Multiplication"))
{
model.result = model.numberOne * model.numberTwo;
}
if (calculateOperation.Equals("Division"))
{
model.result = model.numberOne / model.numberTwo;
}
return View(model);
}
如果我不返回模型对象,我就不会得到 model.result 的值。
寻找正当理由。