2014-09-26 83 views
8

我有一個冗長的表格,我已經打破了幾個部分,並使用@ Html.EditorFor爲每個部分工作很好,但需要你的想法是否可以改進這種方法或不。將一個集合傳遞給EditorFor ASP.NET在ASP.NET MVC

有細分市場,每個細分市場都可以有多個活動,所以我有一個細分市場集合,此集合中的每個細分市場都包含一系列活動。

public class Activity 
    { 
     public string ActivityId { get; set; } 
     public string ActivityDescription { get; set; } 
     public bool IsSelected { get; set; } 
    } 
    public class Segment 
    { 
     public string SegmentId { get; set; } 
     public string SegmentDescription { get; set; } 
     public List<Activity> Activitites { get; set; } 
    } 

這是我怎麼想,我作爲視圖應該看起來像一個模型中使用,但不能做,因爲@ Html.EditorFor它的工作沒有接受集合類型的視圖模型。

public class UserPreferencesViewModel 
    { 
     //..... Other Properties 
     public List<Segment> Segments {get; set;} 
    } 

這裏是視圖模型

@model UserPreferencesViewModel 
@{ 
    //... Other Properties 
    @Html.EditorFor(m => m.Segments) //I assigned Segments three Segments in the Controller Get Method 
} 

這裏是EditorFor模板段

@model List<Segment> 
@{ 
    //... Other Properties 
    @foreach(var segment in Model) 
    { 
     //Do the stuff 
    } 
} 

但這不起作用說EditorFor不能以集合和異常在運行時拋出。

這是我的工作。我創建了另一個類「UglySegmentWorkAround」,其中包含段集合,然後在UserPreferencesViewModel中刪除了列表屬性,而是爲此定義了一個屬性。

public class UglySegmentWorkAround 
{ 
public List<Segment> Segments {get; set;} 
} 

public class UserPreferencesViewModel 
     { 
      //..... Other Properties 
      public UglySegmentWorkAround UglySegmentWorkAround {get; set;} 
     } 

這裏是EditorFor Template。

@model UglySegmentWorkAround 
    @{ 
     //... Other Properties 
     @foreach(var segments in Model.Segments) 
     { 
      //Do the stuff 
     } 
    } 

它完美的作品,但我只是感覺不舒服這種做法,是有什麼我在第一種方法缺少什麼?這應該如何完成?如果我將它傳遞給一個集合,我不想讓EditorFor做一個隱式循環,因爲我正在EditorFor中渲染一個複雜的UI結構,並且我需要EditorFor將其中的循環。

+0

檢查此鏈接(http://stackoverflow.com/questions/13420791/mvc-editorfor-not-working-for-collection) – 2014-09-26 21:41:07

+0

@HasanFahim我不喜歡我不想在EditorFor外寫一個for循環,就像我在我的問題中提到的那樣,而是我的UI需要我循環編輯器中的集合。 – 2014-09-26 22:47:58

回答

28

EditorFor旨在爲您迭代集合。它自動執行此操作。當您將集合傳遞給EditorFor時,它會自動調用集合中每個項目的模板。

如果你需要設置一些渲染的集合作爲一個整體,那麼你應該這樣做的EditorFor通話之外,無論是在你的視圖代碼,或在它調用你的EditorFor的局部視圖。

舉例來說,如果你想要把你的代碼在一個表中,你可以這樣做(其中MyCollection的是List<MyItem>):

_MyCollectionPartial.cshtml

<table> 
    <tr> 
     <th>Foo...</th> 
     ... 
    <tr> 
    @Html.EditorFor(x => x.MyCollection) 
</table> 

/查看/共享/ EditorTemplates/MyItem.cshtml

@model MyItem 
<tr> 
    <td>@Html.TextBox(x => x.Foo)</td> 
    .... 
</tr> 

編輯:

Perhap更好的方法是使用編輯器模板中已知的,但記錄不完善的「特徵」。這個「功能」是,如果你指定一個模板名稱作爲參數,那麼它不會遍歷集合。您可以使用此表單「包裝」您的收藏項目模板。

/Home/Index.cshtml

.... your html 
@Html.EditorFor(model => model.MyCollection, "MyCollectionLayout") 

/Views/Shared/EditorTemplates/MyCollectionLayout.cshtml

@model List<MyItem> 
<table> 
    <tr> 
     <th>Foo...</th> 
     ... 
    <tr> 
    @Html.EditorForModel() (Or alternatively @Html.EditorFor(model => model) 
</table> 

/Views/Shared/EditorTemplates/MyItem.cshtml

@model MyItem 
<tr> 
    <td>@Html.TextBoxFor(x => x.Foo)</td> 
    .... 
</tr> 

注:我說「功能」,因爲這在這裏產生了很多問題關於它沒有迭代收集離子當模板名稱是明確指定的參數)

+4

「這個」功能「是,如果你指定一個模板名稱作爲參數,那麼它不會遍歷集合」非常感謝這一點。 – Jono 2016-03-30 11:37:13

+0

有必要把模板放入文件夾/視圖/共享/ EditorTemplates befor我可以得到它的工作 – 2017-05-22 10:32:47

+0

@LarsLadegaard - 你是對的,不知道我怎麼打錯了。我會在帖子中修復它。我應該注意到EditorTemplates只有在共享的情況下才能進入Shared,如果只有一個控制器需要它們,它們應該放在Controllers Views文件夾中。 – 2017-05-22 13:37:45

相關問題