我正在爲我們公司的第一個MVC3項目工作,並且我碰到了一個障礙。沒有人能夠弄清楚發生了什麼事。單身財產沒有越來越綁定HttpPost
我有一個複雜的模型,我使用的網頁上:,我創建了一個SpaceModels空白SpaceModels對象時,空間得到了聯合
public class SpaceModels : List<SpaceModel> {
public bool HideValidation { get; set; }
[Required(ErrorMessage=Utilities.EffectiveDate + Utilities.NotBlank)]
public DateTime EffectiveDate { get; set; }
public bool DisplayEffectiveDate { get; set; }
}
在控制器(這將是目標空間) 。
// Need a list of the models for the View.
SpaceModels models = new SpaceModels();
models.EffectiveDate = DateTime.Now.Date;
models.DisplayEffectiveDate = true;
models.Add(new SpaceModel { StoreID = storeID, SiteID = siteID, IsActive = true });
return View("CombineSpaces", models);
在查看
然後,我使用的SpaceModels對象作爲模型,並在形式製作的生效日期文本框:
@model FiveBelow.CoTenancy.Data.SpaceModels
@using (Html.BeginForm("CombineSpaces", "Space")) {
<div class="EditLine">
<span class="EditLabel LongText">
New Space Open Date
</span>
@Html.TextBoxFor(m => m.EffectiveDate, new {
size = "20",
@class = "datecontrol",
// Make this as a nullable DateTime for Display purposes so we don't start the Calendar at 1/1/0000.
@Value = Utilities.ToStringOrDefault(Model.EffectiveDate == DateTime.MinValue ? null : (DateTime?)Model.EffectiveDate, "MM/dd/yyyy", string.Empty)
})
@Html.ValidationMessageFor(m => m.EffectiveDate)
</div>
<hr />
Html.RenderPartial("_SpaceEntry", Model);
}
局部視圖即獲得通過的所有渲染迭代SpaceModel,並創建一個包含各個SpaceModel對象的編輯字段。 (我使用的是列表中使用時,空間得到細分,以及對同一查看)
然後在HttpPost,該EFFECTIVEDATE還是回到它的默認DateTime.MinValue:
[HttpPost]
public ActionResult CombineSpaces(SpaceModels model, long siteID, long storeID, DateTime? effectiveDate) {
// processing code
}
我添加了DateTime? effectiveDate參數來證明它發生變化時的值實際上會回來。我甚至嘗試將TextBox的渲染移動到_SpaceEntry部分視圖中,但在那裏也沒有任何工作。
我也嘗試使用@Html.EditorFor(m => m.EffectiveDate)
代替@Html.TextBoxFor()
,但仍然返回了DateTime.MinValue。 (我的老闆不喜歡順便使用@Html.EditorForModel
放棄渲染控制。)
必須有一些簡單的東西我錯過了。如果你需要其他東西,請告訴我。
雖然我打字這個時候,我的一個同事改變'SpaceModels'具有'名單'屬性,而不是從派生,它似乎已經解決了這一問題。我試圖弄明白這一點時,將其視爲短暫的想法,但不明白爲什麼會影響到這一點。 如果有人知道原因,並幫助其他人遇到同樣的事情,我仍然會在這裏留下這個問題。 –
krillgar