2012-08-28 17 views
0

我正在爲我們公司的第一個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放棄渲染控制。)

必須有一些簡單的東西我錯過了。如果你需要其他東西,請告訴我。

+0

雖然我打字這個時候,我的一個同事改變'SpaceModels'具有'名單'屬性,而不是從派生,它似乎已經解決了這一問題。我試圖弄明白這一點時,將其視爲短暫的想法,但不明白爲什麼會影響到這一點。 如果有人知道原因,並幫助其他人遇到同樣的事情,我仍然會在這裏留下這個問題。 – krillgar

回答

1

查看source code對於DefaultModelBinder,特別是BindComplexModel(),如果它檢測到集合類型,它將綁定單個元素,但不會嘗試綁定列表對象本身的屬性。

+0

非常好!非常感謝澄清。 – krillgar

1

模型綁定所做的是嘗試將視圖中的事物或元素的名稱與模型中的屬性或操作方法中的參數進行匹配。您不必傳遞所有這些參數,只需將其添加到視圖模型中,然後在您的操作方法中調用TryUpdateModel即可。我不確定你想要用SpaceModel或List做什麼,但我沒有看到需要從列表繼承。我確定你有這樣做的好理由。這是我會怎麼做的。

視圖模型

public class SpacesViewModel 
{ 
    public DateTime? EffectiveDate { get; set; } 
    public bool DisplayEffectiveDate { get; set; } 
    public List<SpaceModel> SpaceModels { get; set; } 
} 

的GET操作方法

[ActionName("_SpaceEntry")] 
public PartialViewResult SpaceEntry() 
{ 
    var spaceModels = new List<SpaceModel>(); 
    spaceModels.Add(
     new SpaceModel { StoreID = storeID, SiteID = siteID, IsActive = true }); 

    var spacesVm = new SpacesViewModel 
    { 
     EffectiveDate = DateTime.Now, 
     DisplayEffectiveDate = true, 
     SpaceModels = spaceModels 
    }; 

    return PartialView("_SpaceEntry", spacesVm); 
} 

該POST操作方法

[HttpPost] 
public ActionResult CombineSpaces() 
{ 
    var spacesVm = new SpacesViewModel(); 

    // this forces model binding and calls ModelState.IsValid 
    // and returns true if the model is Valid 
    if (TryUpdateModel(spacesVm)) 
    { 
     // process your data here 
    } 
    return RedirectToAction("Index", "Home"); 
} 

和視圖

<label>Effective date: </label> 
@Html.TextBox("EffectiveDate", Model.EffectiveDate.HasValue ? 
    Model.EffectiveDate.Value.ToString("MM/dd/yyyy") : string.empty, 
    new { @class = "datecontrol" }) 

有時需要使用隱藏字段,諸如

@Html.HiddenField("EffectiveDate", Model.EfectiveDate.) 

顯式綁定表格數據爲了結合空間模型的對象的屬性可以如SITEID到視圖模型添加單個屬性或添加一個空間模型屬性一個單一的SpaceModel。如果要成功綁定複雜模型,請將其添加爲填充了鍵值對而不是List的Dictionary。然後,您應該將字典添加到視圖模型。您甚至可以爲分層數據添加字典詞典。

我希望這有助於:)

+0

非常感謝。我的意思是馬上回頭閱讀,但是我還有其他一些事情。 我真的很感謝深度,但最重要的是,你如何說:「我沒有看到需要從列表繼承,我確定你有一個很好的理由去做。」很高興看到有人不想假設他們知道發生的一切。 我不記得以前看過那個TryUpdateModel方法,但我喜歡它的外觀。我一定會檢查出來的。 – krillgar

+0

很高興我能幫忙:) –