문제

고 말할 수 있습니다 나는 다음과 같은 클래스(할 수 있도 고려면 클래스의 측면에서는 관련 부분은 정확한):

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

어떻게 나는 면도기 이를 위해...

내 만들기""프로젝트 페이지 양식이 필요는 다음과 같은 이...하지만 이것은 올바른지?

@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>

그래서 나는 결국 만들기 Shared/EditorTemplates/Address.cshtml 다음 건설 주소 형태:

@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 할 수 없는 경우 그렇게 했 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>

이 보는 정확합니까?나는 그것을 찾을 압도하는 매우 혼란(Btw,내가 이것을 시도하고 그것은 작동하지 않...하지만 사실을 알고 있는 경우에 그것은 올바른 방법으로,그 다음에 밀어 넣는 방향으로)!

도움이 되었습니까?

해결책

당신은 오른쪽에는 트랙에 있습니다.다만주의의 어디에 당신이 당신의 EditorTemplates 에서 프로젝트 구조가 어떻게 당신이 그들을 참조하십시오.MVC 컨벤션은 항상 작동하지 않는 방식으로 희망을 예:편집기를 사용하여 템플릿을 위한 하나의 모델을 때는 모델에 싸여 목록입니다.

한 가지 제안을 내가 사용하는 것이 완전히 분리 보기"모델은"위한 모델을 통과 당신의 면도기 전망이다.할 수 있습도 사이의 전망 모형과 당신의 엔티티 프레임 워크 모델을(즉,데이터 모델)같은 것을 사용하 automapper.왜?당신이 통해 찾을 수 있는 시간 그들이 필요로 다른 것을 예:mvc 특성 모델 속성입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top