1

我想知道什麼是最好的方法是建立一個簡單的網頁,如this mockflow layout。 這不是我對MVC本身的新意。我只是不知道構建頁面的常見方式。看到下面的代碼我將如何處理它。請注意,我只對視圖模型和視圖感興趣。簡單的MVC5網頁架構

的ViewModels

public class FoodModel 
{ 
    public CategoryModel CategoryModel { get; set; } 
    [Display(Name="Pizzas")] 
    public PizzaModel PizzaModel { get; set; } 
    [Display(Name="Sandwiches")] 
    public SandwichModel SandwichModel { get; set; } 
    [Display(Name="Meats")] 
    public MeatModel MeatModel { get; set; } 
} 

public class CategoryModel 
{ 
    public int SelectedCategoryId {get; set; } 
    public string RandomField { get; set; } 
} 

public class PizzaModel 
{ 
    public IList<PizzaRow> PizzaRows {get; set; } 
} 

public class PizzaRow 
{ 
    public string Name { get; set; } 
    public string Ingredients { get; set; } 
} 

public class SandwichModel 
{ 
    public IList<SandwichRow> SandwichRows {get; set; } 
} 

public class SandwichRow 
{ 
    public string Name { get; set; } 
    public string Ingredients { get; set; } 
    public decimal Price { get; set; } 
} 

public class MeatModel 
{ 
    public IList<MeatRow> MeatRows {get; set; } 
} 

public class MeatRow 
{ 
    public string Name { get; set; } 
    public string Animal { get; set; } 
    public int Weight { get; set; } 
} 

索引視圖和每個模型及其行EditorTemplates。

<!-- ~Views/Food/Index.cshtml -just the nessecary razor-code. don't mind the layout--> 
@Model FoodModel 

    @Html.EditorFor(x => x.CategoryModel) @*For generating the top bar*@ 
    <div>@Html.LabelFor(x => PizzaModel)</div>@*For grid name 'Pizzas'*@ 
    @Html.EditorFor(x => x.PizzaModel) 
    <div>@Html.LabelFor(x => SandwichModel)</div>@*For grid name 'Sandwiches'*@ 
    @Html.EditorFor(x => x.SandwichModel) 
    <div>@Html.LabelFor(x => MeatModel)</div>@*For grid name 'Meats'*@ 
    @Html.EditorFor(x => x.MeatModel) 

<!-- ~Views/Food/EditorTemplates/CategoryModel.cshtml -just the nessecary razor-code. don't mind the layout--> 
@Model CategoryModel 

    @Html.DropDownListFor(x => x.SelectedCategoryId, ViewBag.Categories) 
    @Html.DisplayFor(x => x.RandomField) 

<!-- ~Views/Food/EditorTemplates/PizzaModel.cshtml -just the nessecary razor-code. don't mind the layout--> 
@Model PizzaModel 

    <table> 
     <thead> 
      <tr> 
       <th>@Html.LabelFor(x => x.PizzaRows.FirstOrDefault().Name)</th> 
       <th>@Html.LabelFor(x => x.PizzaRows.FirstOrDefault().Ingredients)</th> 
      </tr> 
     </thead> 
     Html.EditorFor(x => x.PizzaRows); 
    </table> 

<!-- ~Views/Food/EditorTemplates/SandwichModel.cshtml -just the nessecary razor-code. don't mind the layout--> 
@Model SandwichModel 

    <table> 
     <thead> 
      <tr> 
       <th>@Html.LabelFor(x => x.PizzaRows.FirstOrDefault().Name)</th> 
       <th>@Html.LabelFor(x => x.PizzaRows.FirstOrDefault().Ingredients)</th> 
       <th>@Html.LabelFor(x => x.PizzaRows.FirstOrDefault().Price)</th> 
      </tr> 
     </thead> 
     Html.EditorFor(x => x.SandwichRows); 
    </table> 


<!-- ~Views/Food/EditorTemplates/MeatModel.cshtml -just the nessecary razor-code. don't mind the layout--> 
@Model MeatModel 

    <table> 
     <thead> 
      <tr> 
       <th>@Html.LabelFor(x => x.PizzaRows.FirstOrDefault().Name)</th> 
       <th>@Html.LabelFor(x => x.PizzaRows.FirstOrDefault().Animal)</th> 
       <th>@Html.LabelFor(x => x.PizzaRows.FirstOrDefault().Weight)</th> 
      </tr> 
     </thead> 
    Html.EditorFor(x => x.MeatRows); 
    </table> 


<!-- ~Views/Food/EditorTemplates/PizzaRow.cshtml -just the nessecary razor-code. don't mind the layout--> 
@Model PizzaRow 

    <tr> 
     <td>@Html.DisplayFor(x => x.Name)</td> 
     <td>@Html.DisplayFor(x => x.Ingredients)</td> 
    </tr> 

<!-- ~Views/Food/EditorTemplates/SandwichRow.cshtml -just the nessecary razor-code. don't mind the layout--> 
@Model SandwichRow 

    <tr> 
     <td>@Html.DisplayFor(x => x.Name)</td> 
     <td>@Html.DisplayFor(x => x.Ingredients)</td> 
     <td>@Html.DisplayFor(x => x.Price)</td> 
    </tr> 

<!-- ~Views/Food/EditorTemplates/MeatRow.cshtml -just the nessecary razor-code. don't mind the layout--> 
@Model MeatRow 

    <tr> 
     <td>@Html.DisplayFor(x => x.Name)</td> 
     <td>@Html.DisplayFor(x => x.Animal)</td> 
     <td>@Html.DisplayFor(x => x.Weight)</td> 
    </tr> 

這是一個體面的方法,或者我是一個愚蠢的使用這些EditorFor的,而不是部分。或者,也許還有其他建議和提示。感謝您的時間和建議。

+0

爲什麼你認爲你是一個傻瓜使用框架提供的工具來完成你想要做的事情?使用部分模板將是愚蠢的選擇。 – 2014-09-29 04:21:45

回答

0

我唯一的評論就是你似乎在使用EdtiorTemplates來顯示數據......改用DisplayTemplates。您應該也可以在第一個項目上使用DisplayNameFor()而不是LabelFor。這就是DisplayNameFor是.. well .. for。