1 Replies - 8147 Views - Last Post: 21 October 2012 - 05:05 PM

#1 Xna4life   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

MVC using edit and delete function in HomeController? for DB

Posted 19 October 2012 - 12:11 PM

Hey guys...

So iv just completed the youtube tutorial http://www.youtube.c...h?v=LAhZ29p9f4k

which shows us how to create a DB, add it to a data model, then use the default methods (create,edit,delete) in homecontroller to use the DB

My code cosists of...so far this allows me to use the Index to create but im running into difficulties when I tried the edit, and delete functions myself

namespace MvcApplication7.Controllers
{
    public class HomeController : Controller
    {
        private MoviesDBEntities _entities = new MoviesDBEntities();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View(_entities.MovieTable1.ToList());
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        //public ActionResult Create(FormCollection collection)
        public ActionResult Create([Bind(Exclude = "id")]MovieTable1 movieToCreate)
        {
            try
            {
                // TODO: Add insert logic here
                _entities.AddToMovieTable1(movieToCreate);
                _entities.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


So...for edit I have used:
// GET: /Home/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        //public ActionResult Edit(int id, FormCollection collection)
        public ActionResult Edit([Bind(Exclude = "id")]MovieTable1 movieToCreate)
        {
            try
            {
                // TODO: Add update logic here
                
               //so i need to insert a line of code here to say something like update
                //I was using the previous code:
               //_entities.AddToMovieTable1(movieToCreate);
                _entities.SaveChanges();           
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


so where i use '_entities.addToMovieTable1(movieToCreate);' in the edit function I have looked for an edit or update selection from the drop down but they don't appear?

_entities.???(movieToCreate)???

Could someone advise me what I am missing

------------------------------------------------------------

below you will find my code for delete...however when i run this it throws me a nullExeptionRef from the .asp page

public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        //public ActionResult Delete(int id, FormCollection collection)
        public ActionResult Delete([Bind(Exclude = "id")]MovieTable1 movieToDelete)
        {
            try
            {
                // TODO: Add delete logic here
                _entities.DeleteObject(movieToDelete);
                _entities.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


and in my delete.aspx page:

        <div class="display-label">Id</div>
        <div class="display-field"><%: Model.Id %></div>
        
        <div class="display-label">Title</div>
        <div class="display-field"><%: Model.Title %></div>



any help would be greatful

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: MVC using edit and delete function in HomeController? for DB

#2 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: MVC using edit and delete function in HomeController? for DB

Posted 21 October 2012 - 05:05 PM

The problem with the delete is that you are trying to delete an object that is not being tracked by the entity framework. You need to pull an instance of the object from the database and pass that object into the delete method.

For the edit to work you will also need to query the database and get an instance of the entity from the database. Then you should be able to use the _entities.ApplyChanges method, or you could just manually set all of the properties of the entity from the database equal to the matching properties of the object submitted from the web page.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1