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。