【问题标题】:ASP .NET MVC 5 - Customer-Address One-To-One relationshipASP .NET MVC 5 - 客户-地址一对一关系
【发布时间】:2016-02-26 03:08:15
【问题描述】:

我查看了这里的论坛,实际上发现了一些类似的问题,但不是相同的问题。类似的解决方案没有给我正确的答案。

我正在使用 ASP .NET MVC 5 使用实体框架和代码优先方法。

我想为客户建模 -> 解决一对一的关系。我建模的是:

客户类

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.Address.Name 来访问视图中的客户地址字段,但是如何将其发布到控制器并保存到数据库中?

提前谢谢你!

【问题讨论】:

  • 使用@Html.EditorFor(m=> m.Address)
  • 我使用了它,实际上它在视图中做了它应该做的事情,但是我应该修改什么以将它传播到控制器呢?在控制器的 Create POST 方法中,我将 CustomerAddress 实体设置为 null,即使我填写了视图中的所有字段。
  • 好的,我刚刚在我的 Create POST 方法中添加了“[Bind(Include = "Id,Name,Country,City,PostalCode,Street,HouseNumber")] 地址地址”并且我有现在访问地址实体,我首先保存,然后执行 Customer.Address = address 并保存正在按我预期工作的客户:)。谢谢;)。

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-5


【解决方案1】:

您不应该让用户输入您正在创建的地址的 ID。 id 应该由数据库设置。 (这应该是评论,但我的声誉水平不允许我在上面评论)。

【讨论】:

  • 当然,为此我在 Address 的 Id 列上设置了 [ScaffoldColumn(false)] 属性,因此它在视图中不可见,并且由数据库分配;)。
  • 好的,很好。但是你绑定了 Id 值,你可以从 Include 中移除这一列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
  • 2015-09-12
  • 2014-04-27
  • 2015-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多