【问题标题】:MVC 5 Creation of foreign key entities during primary entity creationMVC 5 在主实体创建期间创建外键实体
【发布时间】:2019-03-21 17:46:16
【问题描述】:

我对 MVC 和带有 Entity Framework 的 asp.net 完全陌生,所以我确信我的大部分问题源于不知道我正在尝试做的事情的正确术语。

我有一个“地址”数据库表/实体,它是街道地址。我没有在每个具有关联地址的实体中都有街道地址字段,而是在地址表中简单地有一个指向正确地址的外键列。

对于我的位置创建视图模型,我有以下内容

public class LocationCreationViewModel
{
    public address Address { get; set; }
    [Key]
    public location Location { get; set; }

}

可能值得指出的是,View 模型是“手工制作”的,而地址和位置成员对象由实体框架管理

我的创建视图包含所有位置数据,然后包含一个部分地址视图,该视图使用 Html.Partial("_AddressCreate", Model.Location)

当前发生的情况是 Create(model) 函数中返回的 LocationCreationViewModel 是 LocationCreationViewModel 为空,或者每个字段都为空。在任何情况下,视图中的任何数据都不会填充到视图模型中。

我想要发生的是我取回 2 个地址,将它们添加到数据库中的地址表中,然后将我的订单添加到订单数据库,其中 FK 地址字段指向 2 个新创建的地址。

我在 Visual Studio 2017 中使用 MVC5 和 EF6

创建.cshtml

@model MyWeb.Models.LocationCreationViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>location</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Location.name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Location.name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Location.name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Location.address, "address", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("address", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Location.address, "", new { @class = "text-danger" })
        </div>
    </div>
    @Html.Partial("_AddressPartial", Model.Address)

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

_AddressPartial.cshtml

@model MyWeb.Data.address

    <div class="form-horizontal">
        <h4>address</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.country, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.country, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.country, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.locality, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.locality, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.locality, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.postalCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.postalCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.postalCode, "", new { @class = "text-danger" })
            </div>
        </div>


    </div>

LocationController::创建

[MyWebAuthorize(MyWebRole = MyWebRole.Admin)]
        public ActionResult Create()
        {
            ViewBag.address = new SelectList(db.addresses, "id", "country");
            ViewBag.id = new SelectList(db.inventories, "locationID", "locationID");

            LocationCreationViewModel model = new LocationCreationViewModel();
            return View(model);
        }


[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create( LocationCreationViewModel location)
        {
            if (ModelState.IsValid)
            {
                db.addresses.Add(location.Address); //null reference exception here
                db.locations.Add(location.Location);

                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.address = new SelectList(db.addresses, "id", "country", location.Location.address);
            ViewBag.id = new SelectList(db.inventories, "locationID", "locationID", location.Location.id);
            return View(location);
        }

【问题讨论】:

  • 显示_AddressCreate.cshtml 并查看ShipmentViewModel 正在创建的位置。并且,控制器操作 Create 你在哪里 return View("...", new ShipmentViewModel());
  • @AlexanderYakushev 我添加了一些代码。

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


【解决方案1】:

由于视图模型中的字段名称Location,自动绑定程序无法绑定表单数据。我认为这是由于Location 是页面路由的一部分。 LocationController 的 IE,路由将是 /Location/Create

无论如何,将 LocationCreationViewModel::Location 重命名为 LocationCreationViewModel::Locus 可以解决所有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2018-01-16
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多