0
我想從提交中總結兩個數字。 HTTPGET工作正常,但是,我不能顯示它在視圖形式提交..模型在MVC中發佈後沒有綁定?
public class CalculatorController : Controller
{
//
// GET: /Calculator/
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Sum()
{
CalculatorModel model = new CalculatorModel();
model.FirstOperand = 3;
model.SecondOperand = 4;
model.Result = model.FirstOperand + model.SecondOperand;
//Return the result
return View(model);
}
[HttpPost]
public ActionResult Sum(CalculatorModel model)
{
model.Result = model.FirstOperand + model.SecondOperand;
//Return the result
return View(model);
}
}
@model HTMLHelpersDemo.Models.CalculatorModel
@{
ViewBag.Title = "Sum";
}
<h2>Sum</h2>
@using (Html.BeginForm("Sum", "Calculator", FormMethod.Post))
{
<table border="0" cellpadding="3" cellspacing="1" width="100%">
<tr valign="top">
<td>
@Html.LabelFor(model => model.FirstOperand)
@Html.TextBoxFor(model => model.FirstOperand)
</td>
</tr>
<tr valign="top">
<td>
@Html.LabelFor(model => model.SecondOperand)
@Html.TextBoxFor(model => model.SecondOperand)
</td>
</tr>
<tr valign="top">
<td>
@Html.LabelFor(model => model.Result)
@Html.TextBoxFor(model => model.Result)
</td>
</tr>
</table>
<div style="text-align:right;">
<input type="submit" id="btnSum" value="Sum values" />
</div>
}
最初它顯示7 3加4
,但是當我改變了我值併發布它不顯示舊值..在控制器中調試它顯示完美但無法正常顯示
謝謝,我發現了一篇關於此主題的好文章@ https://blogs.msdn.microsoft.com/simonince/2010/05/05/asp-net-mvcs-html-helpers-render -the-錯誤值/ –