用剃刀创造域模型的层次结构形式 [英] Using Razor to create a form for hierarchy of domain models

查看:148
本文介绍了用剃刀创造域模型的层次结构形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有下面的类(可能也考虑,如果我在这部分的关系而言类是正确的):

Lets say I have the following classes (could you also consider if my classes in terms of that relation part is correct):

public class EFDbContext : DbContext
{
    public DbSet<Project> Projects { get; set; }
    public DbSet<Address> Addresses { get; set; } // *
    public DbSet<Country> Countries { get; set; } // *
    // Custom model builder bindings for * coz of the plural issue with EF
}

public class Project
{
    public int ProjectID { get; set; }
    public string Name { get ; set; }

    public int AddressID { get; set; } // Database relation column
    public Address Address { get ; set; }
}

public class Address
{
    public int AddressID { get; set; }
    public string Address1 { get ; set; }    
    public string Address2 { get ; set; }

    public int CountryID { get; set; } // Database relation column
    public Country Country { get ; set; }
}

public class Country
{    
    public int CountryID { get; set; }
    public string Name { get; set; }
}

我怎么会去这样做的剃刀这...

How would I go about doing the Razor for this...

我的创建项目页面将需要一个看起来是这样的一种形式...但是这是不正确的吧?

My "Create Project" page will need a form that looks something like this... but this is not correct right?

@model Project

    <div class="form-group">
        @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address)
        </div>
    </div>

所以,我结束了创建一个共享/ EditorTemplates / Address.cshtml ,然后构造的地址形式:

So I ended up creating a Shared/EditorTemplates/Address.cshtml which then constructed the Address form:

@model Address

    <div class="form-group">
        @Html.LabelFor(model => model.Address1, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address1)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address2, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address2)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Country, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Country)
        </div>
    </div>

和最后的国一...这需要一个预输入(所以添加到 editorfor 其中的一些自定义类,你不能这样做,我做了文本) - 共享/ EditorTemplates / Country.cshtml

And finally the Country one... Which needs a typeahead (so some custom classes added to the editorfor which you can't do so I did textbox ) - Shared/EditorTemplates/Country.cshtml

@model Country

    <div class="form-group">
        @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBox("", Model.Name.ToString(), new { @class = "countries typeahead" })
        </div>
    </div>

这是否看起来是正确的?我觉得非常热烈,混乱(顺便说一句,我已经试过这一点,它不工作...但如果​​我知道这是正确的做法,那么我可以在这个方向推)!

Does this look correct? I find it very overwhelming and confusing (Btw, I have tried this and it's not working... but if I know it's the correct way, then I can push on in that direction)!

推荐答案

您是在正确的轨道上。只是要小心,你把你的EditorTemplates在项目结构以及如何引用它们的。该公约MVC并不总是工作,你会希望例如方式使用编辑模板时,该模型被包裹在一个列表的单一模式。

You're on the right track. Just be careful of where you place your EditorTemplates in the project structure and how you refer to them. The MVC convention doesn't always work the way you would hope e.g. using editor templates for a single model when the model is wrapped in a list.

一个建议我会用一个完全独立的视图模式传递给你的剃须刀的意见模型。您可以使用类似automapper您的视图模型,你的实体框架模型(即数据模型)之间进行映射。为什么?你会发现随着时间的推移,他们会需要不同的东西如MVC属性上的模型属性。

One suggestion I have would be to use a completely separate "view model" for the model passed to your razor views. You can map between your view model and your entity framework model (aka data model) using something like automapper. Why? You'll find over time that they'll require different things e.g. mvc attributes on model properties.

这篇关于用剃刀创造域模型的层次结构形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆