How to Insert and Update two tables using Entity Framework with ASP.NET MVC4

I am create a demo application with ASP.NET MVC4 with these two tables:

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

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

Note : these tables have a one-to-many relationship

And I have views for Details, Edit and Insert and have also a partial view which is use for helping me insert the second table data ( tblCustomerContact ) (This partial view is use in Index page)

After that create a MVC project and then add an ADO.Net Entity Data Model with named NewCustomerInformation.edmx and map the stored procedure.

Controller Code

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(); } return 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);
        }
    }
}

Index

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

Details

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

Edit

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

Partial View _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>

My problem is that when I am trying to insert it will work fine but when trying to update my table it will not update. Please help I am new to ASP.NET MVC.

Thanks!


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

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

上一篇: Android 2.2中的BackupManager&BackupAgent

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