我正在使用 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(); } 返回视图();}
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 的新手。
谢谢!