mkkk
mkkk

Reputation: 1

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1

im trying to get the controller in my mvc application to edit a specific entity from a data model once the user clicks on the edit button, however I can't seem to make it work. I keep getting this error

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[MvcApplication1.Models.New]' but this dictionary requires a model item of type 'MvcApplication1.Models.New'.

what am I doin wrong. is it due to the strongly typed view??

here is my controller:

public ActionResult Edit(int id)
        {
        var productToEdit = from s in _entities.NewSet      // return the story matching the clicked id
                             where s.storyId == id
                             select s;
            return View(productToEdit);
        }

        // POST : Edit
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(New productToEdit)
        {
            try
            {
                var originalNews = (from s in _entities.NewSet
                                    where s.storyId == productToEdit.storyId
                                    select s).FirstOrDefault();
                _entities.ApplyPropertyChanges(originalNews.EntityKey.EntitySetName, productToEdit);
                _entities.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }

can someone give me a few pointers please. Im still new to all of this.

Upvotes: 0

Views: 3367

Answers (1)

Chandu
Chandu

Reputation: 82903

Change your Edit action with Int Parameter to as follows:

public ActionResult Edit(int id)
{
  var productToEdit = from s in _entities.NewSet     
                      where s.storyId == id
                      select s;
   return View(productToEdit.FirstOrDefault());
}

Upvotes: 5

Related Questions