![](/img/trans.png)
[英]The parameters dictionary contains a null entry for parameter 'idd' of non-nullable type 'System.Int32'
[英]The parameters dictionary contains a null entry for parameter 'restaurantId' of non-nullable type 'System.Int32
我是堆棧溢出的新手。我開始創建MVC應用程序,而不是使用Map.Route,我使用綁定屬性來傳遞ID。 下面顯示了錯誤和每個類/組件的代碼。
參數字典中包含“ OdeToFood.Controllers.ResturantReviewsController”中方法“ System.Web.Mvc.ActionResult Index(Int32)”的非空類型“ System.Int32”的參數“ restaurantId”的空條目。 可選參數必須是引用類型,可為空的類型,或者必須聲明為可選參數。
模型
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>
從表面上看,你有行動
public ActionResult Index([Bind(Prefix = "id")] int restaurantId)
刪除綁定屬性
public ActionResult Index(int id)
綁定前綴意味着您將POST / Get參數作為id.restaurantId。 而且您想捕獲默認路由,該路由將從諸如ResturantReviews / Index / 2的url讀取ID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.