استخدام الشفرة لإنشاء نموذج للتسلسل الهرمي لنماذج المجال

StackOverflow https://stackoverflow.com//questions/22012121

سؤال

لنفترض أن لدي الفئات التالية (هل يمكنك أيضا التفكير فيما إذا كانت فصولي من حيث جزء العلاقة هذا صحيحة):

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>

هل هذا يبدو صحيحا?أجد أنه ساحق جدا ومربكة (راجع للشغل ، لقد حاولت هذا وانها لا تعمل...ولكن إذا كنت أعلم أنها الطريقة الصحيحة ، فيمكنني المضي قدما في هذا الاتجاه)!

هل كانت مفيدة؟

المحلول

أنت على الطريق الصحيح.فقط كن حذرا من حيث يمكنك وضع إديتورتيمبلاتس الخاص بك في هيكل المشروع وكيف يمكنك الرجوع إليها.اتفاقية مفك لا تعمل دائما بالطريقة التي تأمل على سبيل المثال.استخدام قوالب المحرر لنموذج واحد عندما يتم تغليف النموذج في قائمة.

سيكون أحد الاقتراحات التي لدي هو استخدام "نموذج عرض" منفصل تماما للنموذج الذي تم تمريره إلى طرق عرض الشفرة.يمكنك تعيين بين نموذج العرض الخاص بك ونموذج إطار الكيان الخاص بك (ويعرف أيضا باسم نموذج البيانات) باستخدام شيء مثل أوتومابر.لم؟ستجد مع مرور الوقت أنها سوف تتطلب أشياء مختلفة على سبيل المثال.سمات مفك على خصائص النموذج.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top