【问题标题】:Simple MVC5 webpage architecture简单的MVC5网页架构
【发布时间】:2014-11-22 11:13:53
【问题描述】:

我想知道最好的方法是构建一个像this mockflow layout 这样的简单网页。 这并不是说我对 MVC 本身不熟悉。我只是不知道构建页面的常用方法。请参阅以下代码,我将如何处理它。注意,我只对视图模型和视图感兴趣。

视图模型

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; }
}

每个模型及其行的索引视图和编辑器模板。

<!-- ~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 而不是 Partials 是个傻瓜。或者也许还有其他建议和技巧。感谢您的时间和建议。

【问题讨论】:

  • 为什么你认为你是个傻瓜,因为你使用框架提供的工具来做你想做的事?使用部分模板将是愚蠢的选择。

标签: asp.net-mvc razor architecture viewmodel mvc-editor-templates


【解决方案1】:

我要发表的唯一评论是,您似乎正在使用 EdtiorTemplates 来显示数据...改用 DisplayTemplates。您可能还应该使用DisplayNameFor() 作为表头,而不是在第一个项目上使用 LabelFor。这就是 DisplayNameFor 的用途……嗯……是为了。

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    相关资源
    最近更新 更多