2013-01-23 140 views
0

Hihi,模型綁定在ASP.NET MVC中的隱藏控件集合

模型綁定與此類(下面的具體,而不是摘要)有困難。 忽略基本屬性,我對綁定感興趣的列表。

public abstract class MessageModel 
{ 
    public string Tag { get; set; } 
    public string Message { get; set; } 
    public int Id { get; set; } 
    public const int DefaultIdValue = Int32.MinValue; 

    public List<LinkModel> Linked { get; set; } 
    public List<LinkModel> NotLinked { get; set; } 

    protected MessageModel() 
    { 
     Id = DefaultIdValue; 
     Linked = new List<LinkModel>(); 
     NotLinked = new List<LinkModel>(); 
    } 

    protected MessageModel(string tag, string message):this() 
    { 
     Tag = tag; 
     Message = message; 
    } 
} 

public class TextModel:MessageModel 
{ 
     public int TextId { get; set; } 

     public TextModel() 
     { 
       TextId = DefaultIdValue; 
     } 
} 

這是提交我得到上提交服務器端(格式化理智):

Tag= 
&Message= 
&NotLinked.index=35fda83a053645e6809bbb8b0ea00103 
&NotLinked.index=14c2e286e28b4c9d8f889fb3eb437e5f 
&NotLinked.%5b35fda83a053645e6809bbb8b0ea00103%5d.RecipientId=1 
&NotLinked.%5b35fda83a053645e6809bbb8b0ea00103%5d.RecipientName=Bob+Biggins 
&NotLinked.%5b14c2e286e28b4c9d8f889fb3eb437e5f%5d.RecipientId=2 
&NotLinked.%5b14c2e286e28b4c9d8f889fb3eb437e5f%5d.RecipientName=Billy+Oswold 
&Submit=Submit 

當函數被調用接受該模型的NotLinked集合設置爲null。 d:

的(相關)輸出HTML看起來像這樣(我嘗試 「人造」 結合:

<ol> and <li> 

用jQuery做走動的東西的工作)

<div id="NotLinkedContainer"> 
    <ol id="NotLinked" name="NotLinked" style="width: 500px;height: 200px"> 
     <li value="1">Bob Biggins 
      <input id="NotLinked_index" name="NotLinked.index" type="hidden" value="a0ab331bee2a461084b686e13a87090b" /> 
      <input id="NotLinked__a0ab331bee2a461084b686e13a87090b__RecipientId" name="NotLinked.[a0ab331bee2a461084b686e13a87090b].RecipientId" type="hidden" value="1" /> 
      <input id="NotLinked__a0ab331bee2a461084b686e13a87090b__RecipientName" name="NotLinked.[a0ab331bee2a461084b686e13a87090b].RecipientName" type="hidden" value="Bob Biggins" /> 
     </li> 
     <li value="2">Billy Oswold 
      <input id="NotLinked_index" name="NotLinked.index" type="hidden" value="d7d294d3174c4bd98d583e92010359e7" /> 
      <input id="NotLinked__d7d294d3174c4bd98d583e92010359e7__RecipientId" name="NotLinked.[d7d294d3174c4bd98d583e92010359e7].RecipientId" type="hidden" value="2" /> 
      <input id="NotLinked__d7d294d3174c4bd98d583e92010359e7__RecipientName" name="NotLinked.[d7d294d3174c4bd98d583e92010359e7].RecipientName" type="hidden" value="Billy Oswold" /> 
     </li> 
    </ol> 
</div> 

任何想法?之前沒有做過這種複雜的綁定,所以我很可能會犯這個簡單的錯誤。

回答

1

嘗試刪除括號前面的點符號,並且由於這不是字典,而是一個列表,您需要使用索引而不是鍵。對於列表在Razor視圖的正確語法應該看起來更像是這樣的:

<div id="NotLinkedContainer"> 
    <ol id="NotLinked" name="NotLinked" style="width: 500px;height: 200px"> 
     <li value="1">Bob Biggins 
      <input id="NotLinked_index" name="NotLinked.index" type="hidden" value="a0ab331bee2a461084b686e13a87090b" /> 
      <input id="NotLinked__a0ab331bee2a461084b686e13a87090b__RecipientId" name="NotLinked[0].RecipientId" type="hidden" value="1" /> 
      <input id="NotLinked__a0ab331bee2a461084b686e13a87090b__RecipientName" name="NotLinked[0].RecipientName" type="hidden" value="Bob Biggins" /> 
     </li> 
     <li value="2">Billy Oswold 
      <input id="NotLinked_index" name="NotLinked.index" type="hidden" value="d7d294d3174c4bd98d583e92010359e7" /> 
      <input id="NotLinked__d7d294d3174c4bd98d583e92010359e7__RecipientId" name="NotLinked[1].RecipientId" type="hidden" value="2" /> 
      <input id="NotLinked__d7d294d3174c4bd98d583e92010359e7__RecipientName" name="NotLinked[1].RecipientName" type="hidden" value="Billy Oswold" /> 
     </li> 
    </ol> 
</div> 

下面是一篇文章,介紹你正在嘗試做的:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

+0

這是。之前[這是問題。謝謝!只要id匹配,也不需要顯示。 :) – Quibblesome