I have a simple MVC controller and view and model (I removed all other codes to figure out the problem) Model is a simple one property class:
public class SiteFileUploadModel
{
[Required]
public int ActivePage { get; set; }
}
视图也非常简单 :
@model Models.SiteFileUploadModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@if (Model != null)
{
using (this.Html.BeginForm("Index", "SiteFileUpload"))
{
@Html.Hidden("ActivePage",Model.ActivePage)
@Model.ActivePage
switch (Model.ActivePage)
{
case 1:
<p>Page 1</p>
break;
case 2:
<p>Page 2</p>
break;
}
<button type="submit" name="actionButtons">Previous</button>
<button type="submit" name="actionButtons">Next</button>
}
}
控制器只有一个方法:
public class SiteFileUploadController : Controller
{
//
// GET: /FileUpload/SiteFileUpload/
[HttpGet]
public ActionResult Index()
{
var model = new SiteFileUploadModel();
model.ActivePage = 1;
return View(model);
}
[HttpPost]
public ActionResult Index(SiteFileUploadModel model, string actionButtons)
{
if (actionButtons == "Next")
{
model.ActivePage++;
}
if (actionButtons == "Previous")
{
model.ActivePage--;
}
return View(model);
}
}
当我运行它并按下下一个时, 我可以看到这个模型。 激活的Page 变成 2, 它在输出上也显示( 它显示 2 和 第 2 页), 但隐藏的值仍然是 1 。 事实上隐藏的值总是 1, 它不跟随活动Page 的真实值 。 我还用同样效果生成隐藏字段来测试它! 问题是什么?