简体   繁体   English

帐户的ASP.NET MVC5实体框架组字段(国家/地区)

[英]ASP.NET MVC5 Entity Framework Group Fields (Country, State) for Account

I have a Company, Country and State classes. 我有公司,国家和州课程。 State is related to Country State and Country are foreign key to Company 国家与国家相关国家和国家是公司的外键

The problem Company View shows both state and Country as separate dropdowns. 公司视图问题将州和国家/地区显示为单独的下拉列表。 But I expect when I select State, corresponding Country should change or vice versa . 但是我希望当我选择州时,相应的国家应该更改,反之亦然 Is this possible in MVC5 EF Model. 在MVC5 EF模型中是否可行?

Company Class 公司类别

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

}

Country Class 国家级

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

State Class 州级

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

}

I have scaffolded and Generated the view and Company View for reference 我已搭建并生成视图和公司视图以供参考

@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 Scripts { @Scripts.Render("~/bundles/jqueryval") } @section脚本{@ Scripts.Render(“〜/ bundles / jqueryval”)}

Any help would be appreciated 任何帮助,将不胜感激

What you need are Cascading Dropdown Lists. 您需要的是级联下拉列表。 Here is an example from CodeProject. 这是CodeProject的示例。

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

Is this possible in MVC5 EF Model. 在MVC5 EF模型中是否可行?

Yes. 是。 But you'll need to do the work (or get (buy) controls that do it for you). 但是您需要完成工作(或获取(购买)为您完成工作的控件)。

The limitation is not MVC, but that the default HTML controls have no concept of being dependent on another. 限制不是MVC,而是默认的HTML控件没有依赖于另一个控件的概念。 Either write code to change the content of the State drop down when the Country drop down changes (this could include filtering the list of states by the selected country), or get thirdy party controls that do so (it is a common requirement so lots of controls out there 1 ). 在“国家/地区”下拉列表更改时编写代码以更改“国家/地区”下拉列表的内容(这可能包括按所选国家/地区过滤州列表),或者获得第三方控制来这样做(这是常见的要求,因此很多控制在那里1 )。


1 But Stack Overflow does not do recommendations of tools. 1但是Stack Overflow并没有推荐工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM