James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

MVC - Calling Action On Another Controller Without Redirecting

8:00 PM
It is not uncommon to want to call action in another controller.  This is just a quick sample of calling another controller, setting a Boolean value, and committing the changes.

[HttpPost]
public ActionResult Edit(MyController1 ctrl1, int someId)
{
     if (ModelState.IsValid)
    {
        db.MyController1.Add(ctrl1);
        db.SaveChanges();

       //Now update the value of another item
      OtherController ctrl2 = db.OtherController.Find(someId);
      ctrl2.SomeBooleanField = true;
      db.Entry(ctrl2).State = EntityState.Modified;
      db.SaveChanges();
     }

     return View(ctrl1);
}































 

0 comments:

 
Toggle Footer
Top