2012-09-07 45 views
1

我有一個ASP.NET MVC 3應用程序將數據綁定到模型。 簡單模型內的物體是完美的像:聯繫人姓名電話ASP.NET MVC綁定到視圖字典

我只需有這樣做:@Html.TextBoxFor(m => m.ContactName)

模型包含未得到我目前的執行綁定Dictionary對象。

任何人都可以給我一個我如何做到這一點的例子嗎?我現在用於綁定到字典的代碼是:

<div> 
@*TABS*@ 
<ul class="nav nav-tabs"> 
@for (var i = 0; i < Model.Translations.Count; i++) 
{ 
<li class="@i == 0 ? 'active' : ''"><a href="#@Model.Translations.Keys.ToList()[i]" data-toggle="tab">@Model.Translations.Keys.ToList()[i]</a></li> 
} 
</ul> 

@*TABCONTENT*@ 
    <div class="tab-content" style="overflow: visible;"> 

    @foreach (var translation in Model.Translations) 
    { 
     for (var i = 0; i < Model.Translations.Count; i++) 
     { 
     <div class="@i == 0 ? 'active' : ''" id="@translation.Value.CultureCode">@translation.Value.Title</div> 
     } 
@Html.TextBoxFor(m => translation.Value.Title[]); 
@Html.TextBoxFor(m => translation.Value.FullDescription); 
@Html.TextBoxFor(m => translation.Value.PreviewDescription); 
} 
</div> 
</div> 

任何幫助都非常感謝。

+0

您是否嘗試過使用@Html.EditorFor而不是TextBoxFor?正如名稱所示,TextBox將始終嘗試呈現文本框以綁定到提供的數據,而EditorFor將嘗試查找最適合匹配的適當模板。它可能沒有這樣的模板,如果它沒有,我仍然會建議使用EditorFor,然後爲自己寫一個模板。它會保持你的觀點更清潔。讓我知道你是否需要我寫一個更詳細的答案。 – Pluc

回答

3

使用 「列表屬性」[指數] .value.id 例如 如果您有:

public IList<KeyValuePair<int,string>> Properties { get; set; } 

你應該寫在視圖:

for (int i = 0; i < Model.Properties.Count; i++) 
{ 
    @Html.EditorFor(item=>Model.Properties[i].Value) 
} 

已更新:

@for (var i=0; i < Model.Translations.Count; i++) { 
     <div id="[email protected](i)"> 
      <div class="@i == 0 ? 'active' : ''" id="@Model.Translations[i].Value.CultureCode">@Model.Translations[i].Value.Title</div> 
      @Html.TextBoxFor(m => Model.Translations[i].Value.Title); 
      @Html.TextBoxFor(m => Model.Translations[i].Value.FullDescription); 
      @Html.TextBoxFor(m => Model.Translations[i].Value.PreviewDescription); } 
     </div> 

    </div> 
     </div> 
+0

您能否檢查一下我的更新並進行一些修改? – Subby

+0

我更新了我的答案,像這樣使用 – Khurshid

0

試試這個,我正在考慮翻譯字典對象:

@foreach (var category in translation) { 

    @Html.TextBoxFor(m => category.Value.Title); 
    @Html.TextBoxFor(m => category.Value.FullDescription); 
    @Html.TextBoxFor(m => category.Value.PreviewDescription); 

    }