Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:11:11+00:00 2026-05-30T13:11:11+00:00

I am working to add a edit function to my MVC 3 EF model

  • 0

I am working to add a edit function to my MVC 3 EF model first project, There is a list and a user is suppose to be able to edit a row of Questions.

My entities are Questions, CoreVaue and SubjectType, CoreValue and SubjectType have many to many relationship to Question.

Note: Dont make the name of CreateViewModel confuse you, I use it on Create and Delete views. 🙂

This is my GET action inside my Controller:

public ActionResult Edit(int id)
{
    Question q = Arep.GetQuestionById(id);
    CreateViewModel model = new CreateViewModel();
    List<SubjectType> subjectypes = Arep.getallS();
    List<CoreValue> corevalues = Arep.getallC();
    model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
    model.CoreValues = new SelectList(corevalues, "CID", "Cname");

    return View(model);
}

This is my methods inside my AdminRepository:

   public Question GetQuestionById(int id)
    {
    return db.Question.SingleOrDefault(m => m.QID == id);
    }

   public void changequestion(Question question)
    {  
        db.ObjectStateManager.ChangeObjectState(question, EntityState.Modified); 
    }

    public List<SubjectType> getallS()
    {
        var Allsubjectypes = from SID in db.SubjectType
                             select SID;
        return Allsubjectypes.ToList();
    }

    public List<CoreValue> getallC()
    {
        var AllCorevalues = from CID in db.CoreValue
                            select CID;
        return AllCorevalues.ToList();
    }

This is my POST action inside my Controller:

 [HttpPost, ActionName("Edit")]
public ActionResult EditConfirmed(CreateViewModel model)
{
    Question question = new Question();
    //question.QID = id;//test
    if (ModelState.IsValid)
    {
        Arep.changequestion(question);
        Arep.save();
        return RedirectToAction("Edit");
    }
    var CoreValueID = int.Parse(model.Cname);
    var SubjectTypeID = int.Parse(model.Sname);
    var getallC = Arep.getbycid(CoreValueID);
    var getallS = Arep.getbysid(SubjectTypeID);



     return View(model);
}

And this is my CreateViwModel:

public string QuestionText { get; set; }
public string Cname { get; set; }
public string Sname { get; set; }

public SelectList SubjectTypes { get; set; }
public SelectList CoreValues { get; set; }

This is basicly my code for editing and I get this eror when I try to change values and submit it:

Current Object State Manager contains no Object Stat Entry that references an object of type NKI3.Models.Question.

I have no clue what casuses this error? :S

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:11:12+00:00Added an answer on May 30, 2026 at 1:11 pm

    In EditConfirmed, I think you want to get an existing question, not create a new question. The new question is not tracked by ObjectContext.

    [HttpPost, ActionName("Edit")]
    public ActionResult EditConfirmed(CreateViewModel model)
    {
        Question question = new Question(); // I think you want to get a existing question here
        //question.QID = id;//test
        if (ModelState.IsValid)
        {
            Arep.changequestion(question);
            Arep.save();
            return RedirectToAction("Edit");
        }
        var CoreValueID = int.Parse(model.Cname);
        var SubjectTypeID = int.Parse(model.Sname);
        var getallC = Arep.getbycid(CoreValueID);
        var getallS = Arep.getbysid(SubjectTypeID);
    
    
    
         return View(model);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing my first online shop in PHP. Now working on add product form
I am working on an .NET MVC 3 project where I have made all
My create and delete operations are working well. But, the Edit function is giving
I'm working on an application where you can click on user names to add
I am working on a basic add/edit/delete form using PEAR DB. My add and
As I'm working to add custom printing to my application, I've settled on using
I am working to add 1 month to date() and save it in my
I'm working on an ASP.Net application and working to add some Ajax to it
I have a form in witch users can add their working hours view them
Been working on a personal add-in for VS 2008 and have been researching automation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.