In my SQL, There are two table which I want to edit on my MVC 4 project. Both of them has content like:
id Title Description
1 title1 desc1
2 title2 desc2
. . .
. . .
I tried to get them in a View to edit both table in same time. Model of table1:
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; }
}
}
Model of table2 has same content only class name is Profile.
Then, set them as list in other class like:
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; }
}
}
My controller is:
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");
}
My View is:
@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>
}
When I press the edit button db.Entry(allprofile).State = System.Data.EntityState.Modified; has an error like:
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.
My you help me to edit these tables in a View?
allprofileto Modified.allprofileis not a database entity, it's a Model. There are many more errors in the code, so once you get past this one I guarantee you will encounter more!