2012-03-15 27 views
2

視圖模型

[Validator(typeof(ProdutoCategoriaValidator))] 
public class ProdutoCategoriaViewModel 
{ 
    [HiddenInput(DisplayValue = false)] 
    public Guid ID { get; set; } 

    public IEnumerable<SelectListItem> Tipos { get; set; } // <<<<------- Is not showing in my view 

    [AdditionalMetadata("data-bind", "event: { change: function(data) { Link(data.Nome()); }}")] 
    public string Nome { get; set; } 

    [DataType(DataType.Url)] 
    [AdditionalMetadata("Prefixo", "Produtos/{tipo-de-produto}#")] 
    public string Link { get; set; } 

    public int? Ordem { get; set; } 

    public ProdutoCategoriaViewModel() 
    { 
     ID = Guid.NewGuid(); 
    } 
} 

解決方案

Solution explorerIEnumerable的在我的視圖模型不與EditorForModel

視圖(_Formulario.cshtml)

@model ProdutoCategoriaViewModel 
@using (Html.BeginForm(null, null, FormMethod.Post, new { id="form-produtocategoria", data_bind = "submit: salvar" })) 
{ 
    @Html.AntiForgeryToken() 

    <legend>@Html.MvcSiteMap().SiteMapTitle()</legend> 

    <fieldset> 
     @Html.ValidationSummary(false, "Verifique os erros abaixo:") 
     @Html.EditorForModel() 
    </fieldset> 

    <div class="buttons"> 
     @Html.ActionLink("Cancelar", "Index") 
     <input type="submit" value="SALVAR" /> 
    </div> 
} 

SelectListItem.cshtml

顯示10
@model IEnumerable<SelectListItem> 

@Html.DropDownListFor(m => m, Model) 

<p>Test</p> 

結果

完整映像:http://i.imgur.com/I7HxA.png

View result

  • 我試圖把屬性 「UIHint」,但仍顯示什麼!

問題

我在做什麼錯?

回答

3

默認情況下,當您使用Html.EditorForModel時,不要期望這會緩解到複雜屬性,例如Tipos屬性,該屬性類型爲IEnumerable<SelectListItem>。布拉德威爾遜在他的blog post(更具體地閱讀淺的潛水與深潛部分接近結束的帖子)解釋了這一點。如果您希望發生這種情況,您需要爲對象類型編寫自定義編輯器模板。

另一種可能性是指定模板名稱:

@Html.EditorFor(x => x.Tipos, "SelectListItem") 

記住,對於SelectListItem編輯器模板是錯誤的,因爲你的DropDownListFor結合模型作爲第一個參數還承擔。不要忘記,這個幫助器的第一個參數必須是一個標量屬性,用來保存選定的值。您需要在視圖模型上爲此設置字符串或整數屬性。第二個參數表示集合。

有關編輯模板的另一個重要方面是,當你有IEnumerable<T>類型的屬性,並呼籲T.cshtml編輯模板,這個編輯器模板必須是強類型的T類,而不是作爲IEnumerable<T>你與你的SelectListItem.cshtml模板一樣。如果您使用UIHint或將模板名稱指定爲EditorFor helper的第二個參數,則這不適用。在這種情況下,模板將被輸入到集合中。

所以回顧一下,您可以實現一個自定義對象編輯器模板,因爲Brad Wilson建議將遞歸到複雜屬性,或者您可以修改您的_Formulario.cshtml視圖以指定EditorFor每個單獨的元素。

+0

感謝您的解釋..我不會這樣做每個屬性,我會至少使用'UIHint'屬性。問題是,即使我添加了'UIHint',沒有任何反應! – ridermansb 2012-03-15 17:06:51

+1

@Riderman de Sousa Barbosa,沒有任何反應,因爲正如我所說的,在默認的對象模板中,ASP.NET MVC不會遞歸到複雜的屬性。所以即使你在你的Tipos屬性上放置一個UIHint,它也會被忽略。您需要修改Brad Wilson顯示的默認Object.cshtml模板。 – 2012-03-15 17:12:09

+0

我明白了。那麼我想避免這種情況,但我必須這樣做。謝謝您的幫助! – ridermansb 2012-03-15 17:15:44

1

A @foreach循環渲染的東西,看起來正確,但結果標記將具有相同的id爲每行的控件。它也不會將可枚舉集合發回模型實例。

有兩種方法來完成這項工作,這樣你有集合中的每個項目的唯一ID,所以該集合水合上回發:

1.使用默認的模板編輯器,而不是一個名爲一個

// editor name parameter must be omitted; default editor template's model type 
// should be a single instance of the child object, not an IEnumerable. This 
// convention looks wrong, but it fully works: 
@Html.EditorFor(parentObject => parentObject.Items) 

2.使用@for循環,而不是一個@foreach

@for (int i = 0; i < parentObject.Items.Count ; i++) { 
    // the model binder uses the indexer to disambiguate the collection items' controls: 
    @Html.EditorFor(c => Model.Items[i], "MyEditorTemplate") 
} 

這不會然而工作,:

// this will error out; the model type will not match the view's at runtime: 
@Html.EditorFor(parentObject => parentObject.Items, "MyEditorTemplate") 

也不會對這樣的:

@foreach(var item in parentObject.Items) { 
    // this will render, but won't post the collection items back with the model instance: 
    @Html.EditorFor(c => item, "MyEditorTemplate") 
} 

的詳細解答這是爲什麼,看一下這個問題:MVC can't override EditorTemplate name when used in EditorFor for child object

相關問題