Change State to EntityState.Modified raised error - asp.net

I have the following Asp.Net MVC 4 scaffold code.
//
// POST: /Detail/Edit/5
[HttpPost]
public ActionResult Edit(Detail detail)
{
var dd = Details.FirstOrDefault(d => d.DetailId == detail.DetailId);
if (dd == null)
{
return HttpNotFound();
}
detail.UpdatedBy = User.Identity.Name;
detail.UpdateTime = DateTime.Now;
if (ModelState.IsValid)
{
_db.Entry(detail).State = EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("Index", new { id = detail.MasterId });
}
return View(dealDetail);
}
However, the line _db.Entry(detail).State = EntityState.Modified; raise the following error. What's the correct way to update the detail line of a master/detail editing scenery?
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

This line:
var dd = Details.FirstOrDefault(d => d.DetailId == detail.DetailId);
will cause loading of Detail entity from the database. Now you have two Details with the same Id but only one (the one loaded by that query) can be used for persistence. You can change your code to:
if (!Details.Any(d => d.DetailId == detail.DetailId))
{
return HttpNotFound();
}
or update the attached detail (dd) for example by:
// All values of detail entity must be set in your HTTP post!
_db.Entry(dd).CurrentValues.SetValues(detail);

Related

Updating records on different tables ASP.NET MVC

In my application from the view someone pressed the Approve button, controller will collect the Main Id of the request. Here I want to update 3rd table Approval_Status column to true. I passed the main Id and got the 3rd table Id which I want to update record to the variable.
int PartyId = db.ApprovalProcess.Where(x => x.Req_Id == id).ToList().First().Id;
and then I wrote this code to pass the value. But it wont work. Can I get a help for this (question will seems like easy to you, but i want to tell you that I'm self learning ASP.NET MVC these days. So some stuff still I couldn't get)
Here is my database structure. Main table name is AppRequest, 2nd table is ApprovalProcess and the 3rd one is Approval_Parties.
This is my current code:
public ActionResult ApproveRequest(int? id)
{
int PartyId = db.ApprovalProcess.Where(x => x.Req_Id == id).ToList().First().Id;
if (ModelState.IsValid)
{
// model.Approved_Date = DateTime.Now;
ApprovalParty approvalParty = new ApprovalParty();
approvalParty.Approve_Status = true;
db.SaveChanges();
return RedirectToAction("Index");
}
}
I think I'm missing the code that which record should update in the table that already assigned that Id to the PartyId.
Something like this would work:
public ActionResult ApproveRequest(int? id)
{
ApprovalProcess approvalProcess = db.ApprovalProcess.FirsOrDefault(x => x.Req_Id == id);
if (approvalProcess != null)
{
ApprovalParty approvalParty = db.Approval_Parties.FirsOrDefault(x => x.ApprovalProcess_Id == approvalProcess.Id);
if (approvalParty != null)
{
approvalParty.Approve_Status = true;
approvalParty.Approved_Date = DateTime.Now;
db.SaveChanges();
return RedirectToAction("Index");
}
}
}

Saving current user name(logged) into Form database ASP.Net MVC

I am newbie in ASP.NET. Is there a possible way to save the current logged user into the form database when the form is submitted?
(i.e) When multiple registered users enter a form and click save their user name is saved into the contents of form database.
I had googled it but couldn't find related topics (maybe I used the wrong keywords). If methods are available kindly suggest link.
[Edit]
I have used asp.net identity for authentication.
My current code for save
public ActionResult NewPR()
{
var depts = _context.Depts.ToList();
var currencytypes = _context.Currencytypes.ToList();
var viewModel = new NewPRViewModel
{
PRview = new PRview(),
Currencytypes = currencytypes,
};
return View("NewPR", viewModel);
}
[HttpPost]
public ActionResult Save(PRview prview)
{
if (!ModelState.IsValid)
{
var viewModel = new NewPRViewModel
{
PRview = prview,
Currencytypes = _context.Currencytypes.ToList()
};
return View("NewPR", viewModel);
}
if (prview.Id == 0)
_context.PRviews.Add(prview);
else
{
var prviewInDb = _context.PRviews.Single(c => c.Id == prview.Id);
prviewInDb.SupplierName = prview.SupplierName;
prviewInDb.Brand = prview.Brand;
prviewInDb.Qty = prview.Qty;
prviewInDb.Unitprice = prview.Unitprice;
}
_context.SaveChanges();
return RedirectToAction("Index", "PRview");
}

Cannot post update to database. not all code paths return a value

[HttpPost]
public ActionResult UpdateDetail(User user)
{
bool Status = false;
string message = "";
// Model Validation
if (ModelState.IsValid)
{
using (UsersDatabaseEntities ude = new UsersDatabaseEntities())
{
var v = ude.Users.Where(a => a.Email == User.Identity.Name).FirstOrDefault();
user = v;
ude.Entry(User).State = EntityState.Modified;
ude.SaveChanges();
}
return View(user);
}
}
I keep on getting an error while saving data to the database.
UpdateDetail worked while retrieving message, but i keep getting error when saving.
Your issue is if your ModelState.IsValid == false, then you are not returning anything. I put a comment in code below where it is.
Depending on what your logic needs to do, would determine what needs to be returned if IsValid == false
public ActionResult UpdateDetail(User user)
{
bool Status = false;
string message = "";
// Model Validation
if (ModelState.IsValid)
{
using (UsersDatabaseEntities ude = new UsersDatabaseEntities())
{
var v = ude.Users.Where(a => a.Email == User.Identity.Name).FirstOrDefault();
user = v;
ude.Entry(User).State = EntityState.Modified;
ude.SaveChanges();
}
// this is your issue, this needs to be outisde the if statement, or you have to do an else and return null (or whatever you need to based off your logic)
return View(user);
}
}
Keep return statement outside of If statement. this would fix your error.If model is valid model updated with user details from database will be pushed to View. other wise same user model will be pushed to the view.
[HttpPost]
public ActionResult UpdateDetail(User user)
{
bool Status = false;
string message = "";
// Model Validation
if (ModelState.IsValid)
{
using (UsersDatabaseEntities ude = new UsersDatabaseEntities())
{
var v = ude.Users.Where(a => a.Email == User.Identity.Name).FirstOrDefault();
user = v;
ude.Entry(User).State = EntityState.Modified;
ude.SaveChanges();
}
}
return View(user);
}

entity-framework error: An object with the same key already exists in the ObjectStateManager

I got thiserror
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same
key.
only because this line
oldProject = db.Projectes.Find(project.ID);
I need it in order to get the object before the edit, and afterwards compare the values changes between before and after the edit, I do it in the third if statement (that compare one values), and in the "SFSiteEmailSend.ProjectEdited" function that only check the changes and send email about it.
By the way, I never change the "oldproject", the error still exist even if I delete all the email funcation and the third(last) if statement.
// POST: /Project/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Project project)
{
if (ModelState.IsValid)
{
// For Email Send - start
string UrlProject = Request.Url.GetLeftPart(UriPartial.Authority) + Url.Action("Detail", "Project", new { id = project.ID });
Project oldProject = new Project();
Project newProject = new Project();
newProject = project;
oldProject = db.Projectes.Find(project.ID);
SFSiteEmailSend.ProjectEdited(oldProject, newProject, UrlProject);
// For Email Send - end
if (oldProject.Finished == false && newProject.Finished == true)
{
project.DateFinished = DateTime.Now;
}
db.Entry(project).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
Your code is full of redundancy. Strip some of it out and it should become easier to spot the error:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Project newProject)
{
if (ModelState.IsValid)
{
string UrlProject = Request.Url.GetLeftPart(UriPartial.Authority)
+ Url.Action("Detail", "Project", new { id = newProject.ID });
//both oldProject and newProject have the same Key but only oldProject
//is attached
Project oldProject = db.Projectes.Find(newProject.ID);
SFSiteEmailSend.ProjectEdited(oldProject, newProject, UrlProject);
if (!oldProject.IsFinished && newProject.IsFinished )
newProject.DateFinished = DateTime.Now;
//now you try to attach another project with the same Key.
//It will fail when you call SaveChanges
db.Entry(newProject).State = EntityState.Modified;
//This is an option:
d.Entry(oldProject).State = EntityState.Detached;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(newProject);
}
or you could load oldProject with AsNoTracking in the first place - as you already found out
try
project.entitykey = oldproject.entitykey before savechanges
I found a solution
Instead
public ActionResult Edit(Project project)
{
Project beforeEditProject = db.Projectes.Find(project.ID);
....
}
}
Use
public ActionResult Edit(Project project)
{
Project beforeEditProject = db.Projectes.AsNoTracking().First(p => p.ID == project.ID);
.....
}

ASP.NET MVC create a new database record

I've been learning asp.net mvc 3. So, basically I'm trying to create a new record in database. However, I'm trying to keep a particular record predefined
public ActionResult Create()
{
var dc = new ServicesDataContext();
var model = new Maping();
var query = (from m in dc.Customers
where m.CustomerId == model.CustomerID
select m.CustomerId);
ViewData["CustomerID"] = query.First();
return View(model);
}
//
// POST: /Customerservice/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="CustomerServiceMappingID")] Maping serviceToCreate, FormCollection form)
{
if (!ModelState.IsValid)
return View();
var dc = new ServicesDataContext();
dc.Mapings.InsertOnSubmit(serviceToCreate);
try
{
dc.SubmitChanges();
}
catch (Exception e)
{
}
try
{
var id = Int32.Parse(form["CustomerID"]);
ViewData["CustomerID"] = id;
return RedirectToAction("Index", new { id = id });
}
catch
{
return RedirectToAction("INDEX", "Home");
}
}
So this is what I did. So, the case is that id value in second action method is what i needed. However the second method gets redirectd to index so viewdata value is lost.And the thing i did in the first crate method is wrong because no value is assigned. So, can u please help me with this problem.
Use TempData, not ViewData - it will be valid until it is read again. Also why are you adding it into viewdata when it is being passed as a parameter in the RedirectToAction?

Resources