ASP.NET MVC5 entītiju ietvara grupas lauki (valsts, štats) kontam

Man ir uzņēmuma, valsts un valsts klases. Valsts ir saistīta ar valsti Valsts un valsts ir uzņēmuma ārējā atslēga

Problēmas uzņēmuma skatā gan valsts, gan valsts tiek rādītas kā atsevišķas nolaižamās izvēlnes. Bet es paredzu, ka, atlasot štatu, attiecīgā valsts jāmaina vai otrādi. Vai tas ir iespējams MVC5 EF modelī?

Uzņēmuma klase

public class Company
{
    public int CompanyID { get; set; }
    [Required]
    [StringLength(100,MinimumLength=3)]
    public string Name { get; set; }
    [StringLength(200)]
    public string Address { get; set; }
    public int? StateID { get; set; }
    public virtual State State { get; set; }
    public int? CountryID { get; set;}
    public virtual Country Country { get; set; }
    public int CompanyTypeID { get; set; }
    public virtual CompanyType CompanyType { get; set; }

}

Lauku klase

public class Country
{
    public int CountryID { get; set; }
    [Required]
    [StringLength(100)]
    public string Name { get; set; }
    public virtual ICollection<State> States { get; set; }
    public virtual ICollection<Company> Companies { get; set; }
}

Valsts klase

public class State
{
    public int StateID { get; set; }
    [Required]
    [StringLength(100)]
    public string Name { get; set; }
    public int CountryID { get; set; }
    public virtual Country Country { get; set; }

    public virtual ICollection<Company> Companies { get; set; }

}

Esmu izveidojis sastatnes un ģenerējis skatu un uzņēmuma skatu uzziņai

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Company</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

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

    <div class="form-group">
        @Html.LabelFor(model => model.StateID, "StateID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("StateID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.StateID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CountryID, "CountryID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CountryID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CountryID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CompanyTypeID, "CompanyTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CompanyTypeID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CompanyTypeID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div> } <div> @Html.ActionLink("Back to List", "Index")

@section Skripti { @Scripts.Render("~/bundles/jqueryval")}

Jebkāda palīdzība būtu pateicīga


person Gopi    schedule 16.07.2015    source avots


Atbildes (2)


Jums nepieciešami kaskādes nolaižamie saraksti. Šeit ir piemērs no CodeProject.

http://www.codeproject.com/Articles/258172/Simple-Implementation-of-MVC-Cascading-Ajax-Drop-D

person LSU.Net    schedule 16.07.2015
comment
Paldies, neatkarīgi no tā, vai ir kāda iespēja, sagrupēt šos divus rekvizītus modelī un kontrollerī, lai Visual studio varētu man ģenerēt kodu. - person Gopi; 16.07.2015
comment
Nē. Jums būs jāieraksta kods. HTML vadīklām nav atkarības jēdziena, kā ir norādījuši citi - person LSU.Net; 16.07.2015

Vai tas ir iespējams MVC5 EF modelī?

Jā. Bet jums būs jādara darbs (vai jāiegādājas (jāiegādājas) vadīklas, kas to dara jūsu vietā).

Ierobežojums nav MVC, bet gan tas, ka noklusējuma HTML vadīklām nav atkarības no citas. Vai nu ierakstiet kodu, lai mainītu valsts nolaižamās izvēlnes saturu, kad mainās valsts nolaižamā izvēlne (tas var ietvert štatu saraksta filtrēšanu pēc atlasītās valsts), vai arī iegūstiet trešās puses vadīklas, kas to dara (tā ir izplatīta prasība, tāpēc daudzi vadīklas 1).


1 Taču Stack Overflow nesniedz rīku ieteikumus.

person Richard    schedule 16.07.2015
comment
Es nevēlos izmantot trešās puses vadīklas. Tātad vienīgā iespēja ir to izdarīt manuāli skatā, vai arī ir kāds risinājums - person Gopi; 16.07.2015