私のSQLでは、MVC 4プロジェクトで編集したいテーブルが2つあります。それらの両方は、のようなコンテンツがあります。Razorエンジンを使用して、MVC 4のビューで複数のSQLテーブルを編集するにはどうすればよいですか?
id Title Description
1 title1 desc1
2 title2 desc2
. . .
. . .
私は同じ時間で、両方のテーブルを編集するためのビューでそれらを取得しようとしました。 TABLE1の モデル:表2の
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace emirhanozkan.Models
{
public partial class PersonInfo
{
[Key]
public int id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
モデルは、クラス名のみがプロフィールで同じコンテンツを持っています。その後
、のような他のクラスのリストとして、それらを設定:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using emirhanozkan.Models;
namespace emirhanozkan.Models
{
public class AllProfile
{
emirhanozkanContext db = new emirhanozkanContext();
public List<PersonInfo> ListPersonInfo { set; get; }
public List<Profile> ListProfile { set; get; }
}
}
私のコントローラは、次のとおりです。
public ActionResult EditProfileInfo()
{
var nl = new AllProfile();
nl.ListPersonInfo = db.PersonInfoes.ToList();
nl.ListProfile = db.Profiles.ToList();
return View(nl);
}
[HttpPost]
public ActionResult EditProfileInfo(AllProfile allprofile)
{
db.Entry(allprofile).State = System.Data.EntityState.Modified;
foreach (var item in allprofile.ListPersonInfo)
{
db.PersonInfoes.Where(x => x.id == item.id);
db.PersonInfoes.Where(x => x.Title == item.Title);
db.PersonInfoes.Where(x => x.Description == item.Description);
}
foreach (var item in allprofile.ListProfile)
{
db.Profiles.Where(x => x.id == item.id);
db.Profiles.Where(x => x.Title == item.Title);
db.Profiles.Where(x => x.Description == item.Description);
}
db.SaveChanges();
return RedirectToAction("ProfileInfo");
}
マイビューは次のとおりです。
@model emirhanozkan.Models.AllProfile
@{
ViewBag.Title = "EditProfileInfo";
}
<h2>EditProfileInfo</h2>
@using (Html.BeginForm(null, null, FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>PersonInfo</legend>
@foreach (var item in Model.ListPersonInfo)
{
@Html.HiddenFor(model => item.id)
<div class="editor-field">
@Html.EditorFor(model => item.Title)
@Html.ValidationMessageFor(model => item.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.Description)
@Html.ValidationMessageFor(model => item.Description)
</div>
}
@foreach (var item in Model.ListProfile)
{
@Html.HiddenFor(model => item.id)
<div class="editor-field">
@Html.EditorFor(model => item.Title)
@Html.ValidationMessageFor(model => item.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.Description)
@Html.ValidationMessageFor(model => item.Description)
</div>
}
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
私は編集ボタンを押してくださいdb.Entry(allprofile).State = System.Data.EntityState.Modified;
のようなエラーがあります:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The entity type AllProfile is not part of the model for the current context.
ビューでこれらのテーブルを編集するのを助けてください。
データベースエンティティ 'allprofile'の状態をModifiedに設定しようとしているため、このエラーが表示されます。 'allprofile'はデータベースエンティティではなく、モデルです。コードにはさらに多くのエラーがあります。一度これを過ぎると、私はあなたがさらに遭遇することを保証します! – markpsmith