简体   繁体   English

ASP .NET MVC 5 - 客户地址一对一的关系

[英]ASP .NET MVC 5 - Customer-Address One-To-One relationship

I've looked into forums here and actually found some similar questions, but not the same ones. 我在这里查看了论坛,实际上发现了一些类似的问题,但不是相同的问题。 Similar solutions did not give me the right answer. 类似的解决方案没有给我正确的答案。

I'm working with ASP .NET MVC 5 using Entity Framework and Code First approach. 我正在使用实体框架和代码优先方法使用ASP .NET MVC 5。

I would like to model Customer -> Address One-To-One relationship. 我想模拟客户 - >地址一对一的关系。 What I've modeled is: 我建模的是:

Customer class 客户类

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

Address class 地址类

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

}

Having this relation modeled scaffolding mechanism gives the following (examples for Create methods): 具有这种关系建模的脚手架机制给出了以下(Create方法的示例):

Customer controller Create method 客户控制器创建方法

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

Customer view Create file 客户视图创建文件

@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>

The result I have now : a Create view with fields for all Customer's data and a DropDown list with already existing Addresses in the database to choose for newly created Customer. 我现在的结果是 :创建一个包含所有客户数据字段的视图和一个DropDown列表,其中包含数据库中已存在的地址,供新创建的客户选择。

Expected result : a Create view with fields for Customer's data and all Address's fields for the user to fill. 预期结果 :创建视图,其中包含客户数据的字段和用户要填写的所有地址字段。 When saving the Customer, new Address entity should be saved and assigned for newly created Customer. 保存客户时,应保存新的地址实体并为新创建的客户分配。

How is the best to achieve that? 如何最好地实现这一目标? I know I can access my Customer's Address's fields in the view by simply doing model.Address.Name for instance, but how can I post it then to the controller and save into database? 我知道我可以通过简单地执行model.Address.Name来访问视图中的客户地址字段,但是如何将其发布到控制器并保存到数据库中?

Thank you in advance! 先感谢您!

You should not let user to enter Id of adress which you are creating. 您不应该让用户输入您正在创建的地址的ID。 Id should be set by database. Id应该由数据库设置。 (This should be a comment but my level of reputation does not allow me comment above). (这应该是一个评论,但我的声誉水平不允许我在上面评论)。

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

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