C# ASP.NETMVC5-客户地址一对一关系
我在这里查看了论坛,发现了一些类似的问题,但不是相同的问题。类似的解决方案并没有给我正确的答案 我正在使用实体框架和代码优先的方法处理ASP.NETMVC5 我想模拟客户->解决一对一关系。我所模拟的是: 客户类别C# ASP.NETMVC5-客户地址一对一关系,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我在这里查看了论坛,发现了一些类似的问题,但不是相同的问题。类似的解决方案并没有给我正确的答案 我正在使用实体框架和代码优先的方法处理ASP.NETMVC5 我想模拟客户->解决一对一关系。我所模拟的是: 客户类别 public class Customer { public int Id { get; set; } public string Name { get; set; } [DisplayName("Middle Name")] public str
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
[DisplayName("Middle Name")]
public string MiddleName { get; set; }
[DisplayName("Last Name")]
public string LastName { get; set; }
public int AddressId { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
}
地址类
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
[DisplayName("Middle Name")]
public string MiddleName { get; set; }
[DisplayName("Last Name")]
public string LastName { get; set; }
public int AddressId { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
}
将此关系建模为脚手架机制提供了以下内容(创建方法的示例):
客户控制器创建方法
// GET: Customers/Create
public ActionResult Create()
{
ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Name");
return View();
}
// POST: Customers/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,MiddleName,LastName,AddressId")] Customer customer)
{
if (ModelState.IsValid)
{
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Name", customer.AddressId);
return View(customer);
}
客户视图创建文件
@model Models.Customer
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Customer</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.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MiddleName, "", 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.AddressId, "AddressId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AddressId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AddressId, "", 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")
</div>
@model Models.Customer
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
顾客
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
@LabelFor(model=>model.MiddleName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.MiddleName,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.MiddleName,“,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.AddressId,“AddressId”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“AddressId”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.AddressId,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
我现在得到的结果是:一个创建视图,其中包含所有客户数据的字段,以及一个下拉列表,其中包含数据库中已存在的地址,以便为新创建的客户选择
预期结果:创建视图,其中包含客户数据字段和用户要填写的所有地址字段。保存客户时,应为新创建的客户保存并分配新地址实体
如何最好地实现这一点?我知道我可以通过简单地执行model.Address.Name来访问视图中客户的地址字段,但是我如何将其发布到控制器并保存到数据库中呢
提前谢谢你 您不应该让用户输入您正在创建的地址的Id。Id应该由数据库设置。(这应该是一条评论,但我的声誉水平不允许我在上面发表评论)。使用@Html.EditorFor(m=>m.Address)我使用了它,实际上它在视图中做了它应该做的事情,但是我应该修改什么来将它传播到控制器?在控制器的Create POST方法中,我将CustomerAddress实体设置为null,即使我填充了视图中的所有字段。好的,我刚刚将“[Bind(Include)(Include=“Id,Name,Country,City,PostalCode,Street,HouseNumber”)]Address Address”添加到我的Create POST方法中,我现在可以访问Address enity,这是我首先保存的,然后执行Customer.Address=Address并保存正在按预期工作的客户:)。谢谢。当然,出于这个目的,我在Address的Id列上设置了[ScaffoldColumn(false)]属性,因此它在视图中不可见,并且由数据库指定;)。好的,那很好。但如果绑定Id值,则可以从Include中删除此列。