ASP .NET MVC 5-客户地址一对一关系 [英] ASP .NET MVC 5 - Customer-Address One-To-One relationship

查看:82
本文介绍了ASP .NET MVC 5-客户地址一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里研究过论坛,实际上发现了一些类似的问题,但并非相同.类似的解决方案并没有给我正确的答案.

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.

我正在使用Entity Framework和Code First方法处理ASP .NET MVC 5.

I'm working with ASP .NET MVC 5 using Entity Framework and Code First approach.

我想为客户->处理一对一关系.我建模的是:

I would like to model Customer -> Address One-To-One relationship. What I've modeled is:

客户类别

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

}

具有这种关系模型的脚手架机制提供了以下内容(创建方法的示例):

Having this relation modeled scaffolding mechanism gives the following (examples for Create methods):

客户控制器创建方法

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

我现在得到的结果:一个创建视图,其中包含所有客户数据的字段,以及一个下拉列表,其中包含数据库中已经存在的地址,可供新创建的客户选择.

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.

预期结果:一个创建视图,其中包含用于客户数据的字段以及用于填充用户的所有地址字段.保存客户时,应保存新的地址实体,并将其分配给新创建的客户.

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.

如何最好地做到这一点?我知道我可以通过简单地执行例如model.Address.Name来访问视图中的客户的地址"字段,但是如何将其发布到控制器并保存到数据库中?

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?

提前谢谢!

推荐答案

您不应让用户输入要创建的地址ID. ID应该由数据库设置. (这应该是评论,但我的名声不允许我在上面发表评论.)

You should not let user to enter Id of adress which you are creating. Id should be set by database. (This should be a comment but my level of reputation does not allow me comment above).

这篇关于ASP .NET MVC 5-客户地址一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆