2016-04-18 4 views
0

新しいスタックオーバーフローが発生しました。MVCアプリケーションの作成を開始し、Map.Routeを使用する代わりにバインディング属性idを渡す。以下は、各クラス/コンポーネントのエラーとコードを示しています。パラメータ辞書には、nullable型のパラメータ 'restaurantId'のnullエントリが含まれています。 'System.Int32

パラメータ辞書パラメータ にNULLエントリを含む 'restaurantId' 非NULL可能タイプ '可能System.Int32' の方法 ため 「OdeToFoodの 'System.Web.Mvc.ActionResult指数(Int32)を'。 Controllers.ResturantReviewsController 'をクリックします。オプションの パラメータは、参照型、null可能型、またはオプションのパラメータである として宣言する必要があります。

モデル

public class ResturantReview 
{ 
    public int Id { get; set; } 
    public int Rating { get; set; } 
    public string Body { get; set; } 
    public string ReviewerName { get; set; } 
    public int ResturantId { get; set; } 

} 

Controller 
public class ResturantReviewsController : Controller 
    { 
     private OdeToFoodDb _db = new OdeToFoodDb(); 

     public ActionResult Index([Bind(Prefix = "id")] int restaurantId) 
     { 
      var restaurant = _db.Resturants.Find(restaurantId); 
      if (restaurant != null) 
      { 
       return View(restaurant); 
      } 
      return HttpNotFound(); 
     } 

     [HttpGet] 
     public ActionResult Create(int restaurantId) 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult Create(ResturantReview review) 
     { 
      if (ModelState.IsValid) 
      { 
       _db.Reviews.Add(review); 
       _db.SaveChanges(); 
       return RedirectToAction("Index", new { id = review.ResturantId }); 
      } 
      return View(review); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       _db.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

ビュー

@model OdeToFood.Models.Resturant 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 
@Html.Partial("View", Model.Reviews) 
<p> 
    @Html.ActionLink("Create New", "Create", new { restaurantId = Model.Id }) 
</p> 
+1

失敗したURLを教えてください。 –

+0

あなたは '' RedirectToAction( "Index"、new {レストランID = review.ResturantId});を返し、 '[Bind(Prefix =" id ")]'を削除するように、 'restaurantId'(' id'ではなく) –

答えて

2

アクション

public ActionResult Index([Bind(Prefix = "id")] int restaurantId) 

バインド属性

を削除を持って見ているものから、

バインド・プレフィックスとは、POST/Getパラメータがid.restaurantIdであることを意味します。そして、あなたはResturantReviews/Index/2のようなURLからIDを読み取るデフォルトルーティングをキャッチしたいと思います

+0

こんにちは、トリックをしたが、私は今それがエラーを取得しています:INSERTステートメントは、FOREIGN KEY制約 "FK_dbo.ResturantReviews_dbo.Resturants_Resturant_Id"と競合しています。競合は、データベース "OdeToFoodDb"、テーブル "dbo.Resturants"、列 'Id'で発生しました。 ステートメントが終了しました。 – user3820867

+0

私のrestaurantreviewモデルクラス内でResturantIdに[Key]を追加する必要があります – user3820867

+0

申し訳ありませんVolodymyrはここにレビュー作成ページのURLがありますID:http:// localhost:52918/Reviews/Create?restaurantId = 7 – user3820867

関連する問題