-1

HI i am doing a MVC 3 application. I am using Entity framwork database approach first method. So all the code is generated automatically. But the problem is when i click on "Edit", "Details" and "Delete" link i get this error.

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'Fin_trial_06.Controllers.AuthorController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters.

My controller code is :

 public ActionResult Details(int? id)
    {
        Author authors = _entities.Authors.Single(n => n.Author_ID == id);
        return View(authors);
    }

public ActionResult Edit(int id)
    {
        return View(_entities.Authors.Find(id));
    }


    // POST: /Author/Edit/

    [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult Edit(int id, Author aut)
    {
        if (id == null)
            return View("AuthorNotFound"); 
        try
        {
            _entities.Entry(aut).State = EntityState.Modified;
            _entities.SaveChanges();

            TempData["Message"] = "You have successfully Editied Author";
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }


 public ActionResult Delete(int id)
    {
        return View(_entities.Authors.Find(id));
    }


    // POST: /Author/Delete/5

    [HttpPost]
    public ActionResult Delete(int id, Author aut)
    {
        try
        {

            _entities.Entry(aut).State = EntityState.Deleted;
            _entities.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

Help Please ?

4

2 回答 2

0

嗯,信息似乎很清楚

 [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult Edit(int id, Author aut)
    {
        if (id == null)

如果 id == null,则无法输入操作。

小后果:

if (id == null) 

没用

解决方案:您是否说 id 是

int ?

或者您不要忘记在您的编辑视图中为您的 ID 放置一个隐藏字段

于 2012-04-05T13:04:10.977 回答
0

在该方法中放置一个断点以查看是否将 id 传递给您的编辑函数。如果不是,那么您的视图是错误的,并且没有传递您要编辑的项目的 ID。如果您发布您的视图代码,我可以告诉您它的失败之处。

于 2012-04-05T13:07:38.267 回答