public class SearchForm {
//Note: Property is nullable
public DateTime? CurrentViewDate {get;set;}
public DateTime StartDate {get;set;}
}
//In the controller
//[GET]
public ActionResult Index()
{
}
//[POST]
public ActionResult Index(SearchForm formModel)
{
if(formModel.CurrentViewDate == null)
formModel.CurrentViewDate = DateTime.Now.AddDays(1d);
else
formModel.CurrentViewDate = formModel.CurrentViewDate.AddDays(1d);
formModel.StartDate = DateTime.Now;
}
//In view
@Html.HiddenFor(c => c.CurrentViewDate).
<input id="SearchForm_CurrentViewDate" name="SearchForm.CurrentViewDate"
type="hidden" value="5/25/2012 11:59:59 PM" />
<input type="submit" name="btnNext" id="btnNext" value="Go Increment" />
但是,当我单击提交时,值确实会增加,但隐藏的字段保留以前的值,它显示正确值的唯一方法是如果这样做 @Html.HiddenFor(c => c.CurrentViewDate.Value) ,然后打开重新发布 formModel.CurrentViewDate 具有空值,因为绑定名称不同。
有任何想法吗?感觉像是从 StartDate 属性而不是 CurrentViewDate 属性获取日期时间值