2012-04-05 11 views
-1

私はMVC 3アプリケーションを行っています。私はエンティティのフレームワークのデータベースのアプローチの最初のメソッドを使用しています。したがって、すべてのコードは自動的に生成されます。しかし、問題は、 "編集"、 "詳細"、 "削除"リンクをクリックしたときです。このエラーが発生します。MVC3自動生成コードエラー

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. 

私のコントローラのコードは次のとおりです。

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(); 
     } 
    } 

助けてください?

+1

あなたの意見を表示できますか? – mattytommo

答えて

0

さて、メッセージはID == nullの場合は、アクションを入力することはできません

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

かなり明確なようです。あなたはidが

int ? 

か、あなたのための隠しフィールドを置くことを忘れてはいけないと言う天気を :

小結果:

if (id == null) 

は無用

ソリューションですあなたの編集ビューのID

0

その中にブレークポイントを入れてくださいあなたの編集機能にIDが渡されているかどうかを確認してください。あなたのビューが間違っていて、編集したいアイテムのIDを渡していない場合。ビューコードを投稿すると、どこに失敗したのかを伝えることができます。

+0

@Raphaelパラメータをヌルにすることは、彼の問題を解決するつもりはありません。彼がヌルIDを編集機能に渡した場合、編集する項目はどのように分かりますか? – JCisar