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

我用这两个表创建一个带有ASP.NET MVC4的演示应用程序:

tblCustomerInformation

CREATE TABLE [dbo].[tblCustomerInformation]
(
    [CustomerID] [int] NOT NULL,
    [CustomerName] [varchar](200) NULL,
    [CustomerFatherName] [varchar](200) NULL,
    [CustomerMotherName] [varchar](200) NULL,
    [CustomerAge] [int] NULL,

    CONSTRAINT [PK_tblCustomerInformation] PRIMARY KEY CLUSTERED ([CustomerID] ASC)
)

tblCustomerContact

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

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

我有详细信息,编辑和插入的意见,并且还有一个局部视图用于帮助我插入第二个表数据( tblCustomerContact )(这个局部视图在索引页中使用)

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

控制器代码

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
            {

_custmerInformationEntities.Entry(tblCustomerQuery).CurrentValues.SetValues(tblCustomerInfo); _custmerInformationEntities.SaveChanges(); _custmerInformationEntities.Entry(tblCustomerInfo).State = EntityState.Modified; _custmerInformationEntities.SaveChanges(); tblCustomerContact tblCust = new tblCustomerContact(); }返回View(); }

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

部分视图_CustomerContact

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

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

谢谢!


如果要更新数据库中的记录,则必须在编辑视图中将contractid作为隐藏键传递。

链接地址: http://www.djcxy.com/p/25475.html

上一篇: How to Insert and Update two tables using Entity Framework with ASP.NET MVC4

下一篇: How to update related entities in Entity Framework