ASP.NET MVC5实体框架组字段(国家,州)的帐户
本文关键字:国家 MVC5 NET 实体 框架 字段 ASP | 更新日期: 2023-09-27 18:03:06
我有公司、国家和州三个类。州与国家有关州和国家是公司的外部关键
问题公司视图将州和国家显示为单独的下拉列表。但是我希望当我选择State时,相应的Country应该改变,反之亦然。这在MVC5 EF模型中可能吗?
公司类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; }
}
国家类
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; }
}
状态类
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; }
}
我已经搭建并生成了视图和Company视图以供参考
@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")}
如有任何帮助,不胜感激
你需要的是级联下拉列表。下面是一个来自CodeProject的例子:
http://www.codeproject.com/Articles/258172/Simple-Implementation-of-MVC-Cascading-Ajax-Drop-D这在MVC5 EF模型中可能吗?
是的。但是你需要自己做这些工作(或者获得(购买)为你做这些工作的控件)。
限制不是MVC,而是默认的HTML控件没有相互依赖的概念。当Country下拉列表发生变化时,要么编写代码来更改State下拉列表的内容(这可能包括按所选国家过滤州列表),要么获得第三方控件来执行此操作(这是一个常见的要求,因此有许多控件1)。
1但是Stack Overflow不做工具推荐