C# MVC ASP.NET创建在单个视图中关联的两个模型

C# MVC ASP.NET创建在单个视图中关联的两个模型,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我使用ASP.NET,我有两个模型,联系人和客户,两者之间是一对一的关系。因此,每个联系人都将添加到联系人实体中,当联系人是客户时,额外的信息将添加到客户实体中 我创建了一个包含这两者的ViewModel。当视图模型在post请求中传递时,我首先添加联系人,这将正确添加,然后我想将具有关系的客户添加到前一个联系人,但我不知道如何操作 [HttpPost] public ActionResult CustomerCreate(CustomerViewModel customerViewModel)

我使用ASP.NET,我有两个模型,联系人和客户,两者之间是一对一的关系。因此,每个联系人都将添加到联系人实体中,当联系人是客户时,额外的信息将添加到客户实体中

我创建了一个包含这两者的ViewModel。当视图模型在post请求中传递时,我首先添加联系人,这将正确添加,然后我想将具有关系的客户添加到前一个联系人,但我不知道如何操作

[HttpPost]
public ActionResult CustomerCreate(CustomerViewModel customerViewModel)
{
    if (ModelState.IsValid)
    {
        try
        {
            using (var db = new ModelContactContainer())
            {
                db.Contact.Add(customerViewModel.contact);
                customerViewModel.customer.Contact = customerViewModel.contact;
                db.Customer.Add(customerViewModel.customer);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        catch
        {
            return CustomerCreate();
        }
    }
    return CustomerCreate();
}
更新

以下是我的模型的类:

连络

Customer.cs

CustomerViewModel.cs

这里是风景

CustomerCreate.cshtml

@model W3SchoolsApplication.Models.CustomerViewModel
@{
ViewBag.Title=“创建客户”;
}
@视图包。标题
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.contact.Name,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.contact.Name,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.contact.Name,“,new{@class=“text danger”})
@LabelFor(model=>model.customer.Something,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.customer.Something,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.customer.Something,“,new{@class=“text danger”})
}

我不确定,但也许您应该更改订单,您可能不需要添加联系人:

        customerViewModel.customer.Contact = customerViewModel.contact;
        db.Contact.Add(customerViewModel.customer);
        db.SaveChanges();

你必须了解幕后发生了什么,才能理解为什么这不起作用

using (var db = new ModelContactContainer())
{
    db.Contact.Add(customerViewModel.contact);
    db.SaveChanges(); // so the above has an ID and the foreign key relationship works

    // this doesn't add anything to the database, it just populates the foreign key relationship
    customerViewModel.customer.Contact = customerViewModel.contact; 
    db.Customer.Add(customerViewModel.customer);
    db.SaveChanges();
}
如果希望整个操作是原子的,则必须将其包装在事务中,如下所示:

using (var db = new ModelContactContainer())
using (var transaction = db.Database.BeginTransaction())
{
    db.Contact.Add(customerViewModel.contact);
    db.SaveChanges();

    customerViewModel.customer.Contact = customerViewModel.contact; 
    db.Customer.Add(customerViewModel.customer);
    db.SaveChanges();

    transaction.Commit();
}

你的密码有什么问题?它不起作用了吗?还是您有错误?@PuR3v1L数据库中的表看起来怎么样?哪一个包含指向另一个的外键?@RaduPorumb实际上,实体框架创建它们的方式,它们中都有对另一个的引用,因为关系是一对一的。@RaduPorumb抱歉,现在我看到你在数据库中说的。在数据库中,customer表中有一个外键指向contacts表,这就是我需要它的原因,因为我将添加更多的模型(员工、供应商等),一个人可以同时做2到3件事。请尝试关闭延迟加载-您的解决方案不起作用,它不会添加到数据库中。请查看我的最新帖子,非常感谢您的帮助。:)我找到了答案,请看我的评论,谢谢,但我必须明确添加联系人,以便正确填充外键属性。:)
@model W3SchoolsApplication.Models.CustomerViewModel
@{
    ViewBag.Title = "Create Customer";
}

<h2>@ViewBag.Title</h2>

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

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.contact.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.contact.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.contact.Name, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.customer.Something, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.customer.Something, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.customer.Something, "", 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>
}
        customerViewModel.customer.Contact = customerViewModel.contact;
        db.Contact.Add(customerViewModel.customer);
        db.SaveChanges();
using (var db = new ModelContactContainer())
{
    db.Contact.Add(customerViewModel.contact);
    db.SaveChanges(); // so the above has an ID and the foreign key relationship works

    // this doesn't add anything to the database, it just populates the foreign key relationship
    customerViewModel.customer.Contact = customerViewModel.contact; 
    db.Customer.Add(customerViewModel.customer);
    db.SaveChanges();
}
using (var db = new ModelContactContainer())
using (var transaction = db.Database.BeginTransaction())
{
    db.Contact.Add(customerViewModel.contact);
    db.SaveChanges();

    customerViewModel.customer.Contact = customerViewModel.contact; 
    db.Customer.Add(customerViewModel.customer);
    db.SaveChanges();

    transaction.Commit();
}