2012-01-20 28 views
2

我有一個模型,其中有多個列表,我可以在我的視圖中顯示列表中的所有信息,但是當我將它發回時,所有列表都是空值。我的模型如下(簡潔起見)。MVC3模型不從一個表單發回列表<CustomObject>

public class PartDetail 
{ 
    public string DateCreated { get; set; } 
    [StringLength(255, MinimumLength = 3)] 
    public string Description { get; set; } 
    public Guid ID { get; set; } 
    public string IsActive { get; set; } 
    public string Manufacturer { get; set; } 
    public IEnumerable<SelectListItem> Manufacturers { get; set; } 
    [StringLength(100, MinimumLength = 3)] 
    public string Name { get; set; } 
    public string PartType { get; set; } 
    [Required] 
    [StringLength(100, MinimumLength = 3)] 
    public string SerialNumber { get; set; } 
    public List<DateAttribute> DateAttributes { get; set; } 
    public List<PointAttribute> PointAttributes { get; set; } 
    public List<TextAttribute> TextAttributes { get; set; } 
    public List<NumericAttribute> NumericAttributes { get; set; } 
    public List<LookupAttribute> LookupAttributes { get; set; } 
    public List<UtilizedPartAttribute> PartAttributes { get; set; } 
    public List<MapAttribute> MapAttributes { get; set; } 
} 

我的帖子控制器是

[HttpPost] 
public virtual PartialViewResult UpdatePart(PartDetail part) 
{ 
    part.Update(); 
    return PartList(part.PartType); 
} 

而我的觀點是(縮短爲簡潔起見)

@using RIS.Models.PartModels 
@model PartDetail 

@using (Ajax.BeginForm("UpdatePart", "Part", null, new AjaxOptions { UpdateTargetId = "update_panel" })) 
{ 
    <h3>Part: @Model.SerialNumber</h3> 
    <p class="row"> 
     <label class="span2"> Name:</label> 
     <strong class="span8"> 
      @Html.EditorFor(model => model.Name) 
      @Html.ValidationMessageFor(model => model.Name) 
     </strong> 
    </p> 
....more boilerplate code.... 
    <h3>Attributes</h3> 
    foreach (var date in Model.DateAttributes) 
    { 
     if (date.IsActive) 
     { 
      <div class="row inline-inputs"> 
       <label class="span5">@date.DisplayName:</label> 
       @Html.TextBoxFor(model => date.Value, new { @class = "mini" }) 
       @Html.ValidationMessageFor(model => date.Value)    
      </div> 
     } 
    } 
...more boilerplate code that is very similar to the above for the different lists 
<p> 
    <input type="submit" class="btn primary" value="Save Part"/>     
</p> 

@Html.HiddenFor(model => model.ID) 
@Html.HiddenFor(model => model.Manufacturer) 
@Html.HiddenFor(model => model.DateCreated) 
@Html.HiddenFor(model => model.Manufacturer) 
@Html.HiddenFor(model => model.IsActive) 
@Html.HiddenFor(model => model.PartType) 

}

+0

你在做任何模型綁定? –

+0

我不完全確定你指的是什麼樣的模型綁定。 – PlTaylor

+0

您可以重寫MVC通過編寫自定義聯編程序來判斷您傳遞的內容的方式。你只需要創建一個實現'DefaultModelBinder'的類。如果你有足夠的時間,這是一件好事,因爲最終你會遇到一個模型,只是沒有幫助而無法通過 –

回答

5

使用對於循環,而不是的foreach,在案例模型聯編程序可以區分字段併爲列表li恢復值ke這個Html.TextboxFor(model => model.DateAttributes [i])

+0

這工作。 foreach不可以這樣綁定嗎? – PlTaylor

相關問題