如何在ASP.NET MVC中创建包含外键在我们类中的类的所有对象的下拉列表
我想为如何在ASP.NET MVC中创建包含外键在我们类中的类的所有对象的下拉列表,.net,asp.net-mvc,database,entity-framework,.net,Asp.net Mvc,Database,Entity Framework,我想为Person模型的Edit视图中的字段Status-Status创建一个下拉列表。我在这里看到了使用ViewBag的解决方案:但我不知道如何使它们适应我的情况 我有一个班上的人: namespace WebApplication2.Models { public class Person { public int Id { get; set; } [DisplayName("First Name")] public string Fi
Person
模型的Edit
视图中的字段Status-Status
创建一个下拉列表。我在这里看到了使用ViewBag的解决方案:但我不知道如何使它们适应我的情况
我有一个班上的人:
namespace WebApplication2.Models {
public class Person {
public int Id { get; set; }
[DisplayName("First Name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
public string LastName { get; set; }
[DisplayName("Cell Number")]
public string CellNumber { get; set; }
[DisplayName("Secondary Number")]
public string SecondaryPhoneNumber { get; set; }
[DisplayName("Address")]
public string Address { get; set; }
[DisplayName("Date of Birth")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime BirthDate { get; set; }
[DisplayName("Pesel")]
public string Pesel { get; set; }
[DisplayName("Notes")]
public string Notes { get; set; }
public virtual ICollection<Meeting> Meetings { get; set; }
public Status Status { get; set; }
}
}
namespace WebApplication2.Models{
公共阶层人士{
公共int Id{get;set;}
[显示名称(“名字”)]
公共字符串名{get;set;}
[显示名称(“姓氏”)]
公共字符串LastName{get;set;}
[显示名称(“单元号”)]
公共字符串CellNumber{get;set;}
[显示名称(“辅助编号”)]
公共字符串SecondaryPhoneNumber{get;set;}
[显示名称(“地址”)]
公共字符串地址{get;set;}
[显示姓名(“出生日期”)]
[DisplayFormat(DataFormatString=“{0:d}”,ApplyFormatInEditMode=true)]
公共日期时间出生日期{get;set;}
[显示名称(“Pesel”)]
公共字符串Pesel{get;set;}
[显示名称(“注释”)]
公共字符串注释{get;set;}
公共虚拟ICollection会议{get;set;}
公共状态状态{get;set;}
}
}
我有阶级地位:
namespace WebApplication2.Models {
public partial class Status {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> M { get; set; }
}
}
namespace WebApplication2.Models{
公共部分类状态{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection M{get;set;}
}
}
我已经通过实体框架创建了具有读写操作的控制器。此控件具有视图编辑功能:
@model WebApplication2.Models.Person
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CellNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CellNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CellNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SecondaryPhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SecondaryPhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SecondaryPhoneNumber, "", 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.BirthDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Pesel, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Pesel, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Pesel, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.Status, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.EditorFor(mode => Model.Status);
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model WebApplication2.Models.Person
@{
ViewBag.Title=“编辑”;
}
编辑
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
人
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.Id)
@LabelFor(model=>model.FirstName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”})
@LabelFor(model=>model.LastName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.LastName,“,new{@class=“text danger”})
@LabelFor(model=>model.CellNumber,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CellNumber,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CellNumber,“,new{@class=“text danger”})
@LabelFor(model=>model.SecondaryPhoneNumber,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.SecondaryPhoneNumber,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.SecondaryPhoneNumber,“,new{@class=“text danger”})
@LabelFor(model=>model.Address,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Address,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Address,“,new{@class=“text danger”})
@LabelFor(model=>model.BirthDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.BirthDate,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.BirthDate,“,new{@class=“text danger”})
@LabelFor(model=>model.Pesel,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Pesel,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.Pesel,“,new{@class=“text danger”})
@LabelFor(model=>model.Notes,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Notes,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Notes,“,new{@class=“text danger”})
@LabelFor(model=>model.Status,htmlAttributes:new{@class=“controllabel col-md-2”})
@*@EditorFor(model=>model.Status,new{htmlAttributes=new{@class=“form control”})*@
@EditorFor(mode=>Model.Status);
@Html.ValidationMessageFor(model=>model.Status,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
我为数据库提供了状态实例:
public class PersonInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<PersonContext> {
protected override void Seed(PersonContext context) {
var persons = new List<Person> {
new Person{FirstName = "John", LastName = "Doe", CellNumber = "123-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "312312312", Notes = "Annoying"},
new Person{FirstName = "Anna", LastName = "Doe", CellNumber = "113-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "548555672", Notes = "Less Annoying"}
};
persons.ForEach(person => context.Persons.Add(person));
context.SaveChanges();
var meetings = new List<Meeting>{
new Meeting{PersonId = 1, Body = "Body of meeting", Date = DateTime.Now}
};
meetings.ForEach(meeting => context.Meetings.Add(meeting));
context.SaveChanges();
var statuses = new List<Status> {
new Status{Name = "OK"},
new Status {Name = "NOT_OK"}
};
statuses.ForEach(status => context.Statuses.Add(status));
context.SaveChanges();
}
}
公共类PersonInitializer:System.Data.Entity.DropCreateDatabaseIfModelChanges{
受保护的覆盖无效种子(PersonContext上下文){
var persons=新列表{
新人{FirstName=“John”,LastName=“Doe”,CellNumber=“123-456-789”,SecondaryPhoneNumber=“98873213”,Address=“1stre”
var statusChoices = db.Statuses.Select(m => new SelectListItem
{
Text = m.FooProperty,
Value = m.BarProperty
});
@Html.DropDownListFor(m => m.Status, Model.StatusChoices)
namespace WebApplication2.Models {
public class Person {
public int Id { get; set; }
...
/*public Status Status { get; set; }*/
public int StatusId { get; set; }
public IList<SelectListItem> AllStatuses { get; set; }
public Person ()
{
AllStatuses = new List<SelectListItem>();
}
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new Person();
model.AllStatuses = new List<SelectListItem>
{
new SelectListItem { Text = "One", Value = "1"},
new SelectListItem { Text = "Two", Value = "2"},
new SelectListItem { Text = "Three", Value = "3"}
};
return View(model);
}
[HttpPost]
public ActionResult Index(Person model)
{
// Get the selected value
int id = model.StatusId;
return View();
}
}
@Html.DropDownListFor(x => x.StatusId, Model.AllStatuses)
public int? StatusId { get; set; }
public virtual Status Status { get; set; }
ViewBag.StatusId = new SelectList(db.Statuses, "Id", "Name", person.StatusId);
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,CellNumber,SecondaryPhoneNumber,Address,BirthDate,Pesel,Notes,StatusId")] Person person) {
<div class="form-group">
@Html.LabelFor(model => model.StatusId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("StatusId", String.Empty)
@Html.ValidationMessageFor(model => model.StatusId)
</div>
</div>