0

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?

1
  • 1
    You're seeing that error because you're trying to set the state of a database entity allprofile to Modified. allprofile is 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! Commented Jul 11, 2017 at 15:03

2 Answers 2

0

Agree with markpsmith. My suggestion is to create viewmodel which will have same properties as your entity. In controller map view model with two of your entities then go save to database one by one entity.

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I can see there is one error in which you are trying to set state of a model to modified. Note that there is no need to explicitly do that. When you modify your entities, entity do that for you.

Other than that in your post action you are not actually modifying anything. I think this is what you are trying to do.

 [HttpPost] 
public ActionResult EditProfileInfo(AllProfile allprofile) { 
foreach (var item in allprofile.ListPersonInfo) { 
var dbPerson = db.PersonInfoes.FirstOrDefault(x => x.id == item.id);
 dbPerson.Title = item.Title;
    dbPerson.Title = item.Description; } 
        db.SaveChanges();
 return RedirectToAction("ProfileInfo"); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.