Asp.net mvc 4 如何在ASP.NET MVC4中使用实体框架插入和更新两个表

Asp.net mvc 4 如何在ASP.NET MVC4中使用实体框架插入和更新两个表,asp.net-mvc-4,stored-procedures,entity-framework-4,Asp.net Mvc 4,Stored Procedures,Entity Framework 4,我正在使用ASP.NET MVC4创建一个演示应用程序,其中包含以下两个表: 表TBL客户信息: 表TBL客户联系人: 注意:这些表具有一对多关系 我有查看详细信息、编辑和插入的视图,还有一个局部视图,用于帮助我插入第二个表数据tblCustomerContact此局部视图用于索引页 之后,创建一个MVC项目,然后添加一个名为NewCustomerInformation.edmx的ADO.Net实体数据模型,并映射存储过程 控制器代码 _CustomerInformation Entities.

我正在使用ASP.NET MVC4创建一个演示应用程序,其中包含以下两个表:

表TBL客户信息:

表TBL客户联系人:

注意:这些表具有一对多关系

我有查看详细信息、编辑和插入的视图,还有一个局部视图,用于帮助我插入第二个表数据tblCustomerContact此局部视图用于索引页

之后,创建一个MVC项目,然后添加一个名为NewCustomerInformation.edmx的ADO.Net实体数据模型,并映射存储过程

控制器代码

_CustomerInformation Entities.EntrytblCustomerQuery.CurrentValues.SetValuestblCustomerInfo; _customerinformationentities.SaveChanges; _CustomerInformation Entities.EntrytblCustomerInfo.State=EntityState.Modified; _customerinformationentities.SaveChanges; tblCustomerContact tblCust=新tblCustomerContact; } 返回视图; }

        public ActionResult getList()
        {
            return View(_custmerInformationEntities.tblCustomerInformations);
        }

        public ActionResult Edit(int id = 0)
        {
            tblCustomerInformation _tblCustomerInformation = _custmerInformationEntities.tblCustomerInformations.Find(id);
            tblCustomerContact tblCont = _custmerInformationEntities.tblCustomerContacts.Single(p => p.CustomerId == id);
            return View(_tblCustomerInformation);
        }
    }
}
索引

细节

编辑

部分视图\u客户联系人

我的问题是,当我尝试插入时,它可以正常工作,但当尝试更新我的表时,它不会更新。请帮助我,我是ASP.NET MVC新手


谢谢

若要更新数据库中的记录,必须在“编辑”视图中将CONSTRACTID作为隐藏键传递

CREATE TABLE [dbo].[tblCustomerContact]
(
    [CustomerId] [int] NOT NULL,
    [CustomerContactId] [int] NOT NULL,
    [CustomerAddress] [varchar](100) NULL,
    [CustomerContactNumber] [varchar](20) NULL,
    [CustomerPinCode] [varchar](10) NULL,

    CONSTRAINT [PK_tblCustomerContact] PRIMARY KEY CLUSTERED ([CustomerContactId] ASC)
)

ALTER TABLE [dbo].[tblCustomerContact] WITH CHECK 
ADD CONSTRAINT [FK_tblCustomerContact_tblCustomerInformation] 
FOREIGN KEY([CustomerId]) REFERENCES [dbo].[tblCustomerInformation] ([CustomerID])
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcStoredProcedureApp.Models;
using System.Data;
using System.Data.Entity.Infrastructure;

namespace MvcStoredProcedureApp.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        NewCutomerInformationEntities _custmerInformationEntities = new NewCutomerInformationEntities();
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(tblCustomerInformation tblCustomerInfo, tblCustomerContact tblCustomerCon)
        {
            tblCustomerInformation tblCustomerQuery = _custmerInformationEntities.tblCustomerInformations.Find(tblCustomerInfo.CustomerID);
            if (tblCustomerQuery == null)
            {
                _custmerInformationEntities.tblCustomerInformations.Add(tblCustomerInfo);
                _custmerInformationEntities.SaveChanges();
                _custmerInformationEntities.tblCustomerContacts.Add(tblCustomerCon);
                _custmerInformationEntities.SaveChanges();
            }
            else
            {
        public ActionResult getList()
        {
            return View(_custmerInformationEntities.tblCustomerInformations);
        }

        public ActionResult Edit(int id = 0)
        {
            tblCustomerInformation _tblCustomerInformation = _custmerInformationEntities.tblCustomerInformations.Find(id);
            tblCustomerContact tblCont = _custmerInformationEntities.tblCustomerContacts.Single(p => p.CustomerId == id);
            return View(_tblCustomerInformation);
        }
    }
}
@model MvcStoredProcedureApp.Models.tblCustomerInformation
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { @id = "EntryForm" }))
{
    <table>
        <tr>
            <td>@Html.Label("Customer Id::")</td>
            <td>@Html.TextBoxFor(m=>m.CustomerID)</td>
        </tr>
        <tr>
            <td>@Html.Label("Customer Name")</td>
            <td>@Html.TextBoxFor(m => m.CustomerName)</td>
        </tr>
        <tr>
            <td>@Html.Label("Father Name")</td>
            <td>@Html.TextBoxFor(m => m.CustomerFatherName)</td>
        </tr>
        <tr>
            <td>@Html.Label("Mother Name")</td>
            <td>@Html.TextBoxFor(m => m.CustomerMotherName)</td>
        </tr>
        <tr>
            <td>@Html.Label("Age")</td>
            <td>@Html.TextBoxFor(m => m.CustomerAge)</td>
        </tr>
        <tr>
            <td colspan="2">

                @Html.Partial("_CustomerContact")
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Save" />
            </td>
            <td>
                @Html.ActionLink("List", "getList")
            </td>
        </tr>
    </table>
}
@model IEnumerable<MvcStoredProcedureApp.Models.tblCustomerInformation>

@{
    ViewBag.Title = "getList";
}

<h2>getList</h2>
@using (Html.BeginForm("Edit", "Home",null ,FormMethod.Post, new { @id = "frmTblInformationEdit" }))
{
    <table>
        <tr>
            <td></td>
        </tr>
        @foreach(var item in Model)
        {
            <tr>
                <td>@Html.ActionLink(item.CustomerName, "Edit", new {@id=item.CustomerID})</td>
            </tr>
        }


    </table>
}
@model MvcStoredProcedureApp.Models.tblCustomerInformation

@{
    ViewBag.Title = "Edit";
}
@using  MvcStoredProcedureApp.Models
<h2>Edit</h2>
@using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { @id = "EntryForm" }))
{
    //var tblCutomerInfo = Model.tblCustomerInformations;

    //var tblCust = Model.tblCustomerContacts;
    <table>
        <tr>
            <td>@Html.Label("Customer Id::")</td>
            <td>@Html.TextBoxFor(m=>m.CustomerID)</td>
        </tr>
        <tr>
            <td>@Html.Label("Customer Name")</td>
            <td>@Html.TextBoxFor(m => m.CustomerName)</td>
        </tr>
        <tr>
            <td>@Html.Label("Father Name")</td>
            <td>@Html.TextBoxFor(m => m.CustomerFatherName)</td>
        </tr>
        <tr>
            <td>@Html.Label("Mother Name")</td>
            <td>@Html.TextBoxFor(m => m.CustomerMotherName)</td>
        </tr>
        <tr>
            <td>@Html.Label("Age")</td>
            <td>@Html.TextBoxFor(m => m.CustomerAge)</td>
        </tr>
        <tr>
            <td colspan="2">
                @{
                   tblCustomerContact tblCont = Model.tblCustomerContacts.AsQueryable().Where(p => p.CustomerId == Model.CustomerID).Single();
                }
                @Html.Partial("_CustomerContact",tblCont)
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Save" />
            </td>
            <td>
                @Html.ActionLink("List", "getList")
            </td>
        </tr>
    </table>
}
@model MvcStoredProcedureApp.Models.tblCustomerContact

<table>
    <tr>
        <td>@Html.Label("Contact ID")</td>
        <td>@Html.TextBoxFor(m => m.CustomerContactId)</td>
    </tr>
    <tr>
        <td>@Html.Label("Contact Number")</td>
        <td>@Html.TextBoxFor(m => m.CustomerContactNumber)</td>
    </tr>
    <tr>
        <td>@Html.Label("Customer Address")</td>
        <td>@Html.TextBoxFor(m => m.CustomerAddress)</td>
    </tr>
    <tr>
        <td>@Html.Label("Pin-Code")</td>
        <td>@Html.TextBoxFor(m => m.CustomerPinCode)</td>
    </tr>

</table>