次の 2 つのテーブルを使用して、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])
注: これらのテーブルには 1 対多の関係があります
また、詳細、編集、挿入のビューがあり、2 番目のテーブル データを挿入するのに役立つ部分ビューもありますtblCustomerContact
(この部分ビューはインデックス ページで使用されます)。
その後、MVC プロジェクトを作成し、ADO.Net エンティティ データ モデルを名前付きで追加しNewCustomerInformation.edmx
、ストアド プロシージャをマップします。
コントローラーコード
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
{
_customerInformationEntities.Entry(tblCustomerQuery).CurrentValues.SetValues(tblCustomerInfo); _customerInformationEntities.SaveChanges(); _customerInformationEntities.Entry(tblCustomerInfo).State = EntityState.Modified; _customerInformationEntities.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 を初めて使用するのを手伝ってください。
ありがとう!