[英]The parameters dictionary contains a null entry for parameter 'restaurantId' of non-nullable type 'System.Int32
Im new to stack overflow.I started to create an MVC application and instead of using a Map.Route I use a binding attribute to pass in the id. 我是堆栈溢出的新手。我开始创建MVC应用程序,而不是使用Map.Route,我使用绑定属性来传递ID。 Below shows the error and the code for each class/components.
下面显示了错误和每个类/组件的代码。
The parameters dictionary contains a null entry for parameter 'restaurantId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'OdeToFood.Controllers.ResturantReviewsController'.
参数字典中包含“ OdeToFood.Controllers.ResturantReviewsController”中方法“ System.Web.Mvc.ActionResult Index(Int32)”的非空类型“ System.Int32”的参数“ restaurantId”的空条目。 An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。
Model 模型
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);
}
View 视图
@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>
From what is see you have action 从表面上看,你有行动
public ActionResult Index([Bind(Prefix = "id")] int restaurantId)
Remove Bind attribute 删除绑定属性
public ActionResult Index(int id)
Bind prefix means that you POST/Get parameter as id.restaurantId. 绑定前缀意味着您将POST / Get参数作为id.restaurantId。 And you want to catch default routing which will read id from url like ResturantReviews/Index/2
而且您想捕获默认路由,该路由将从诸如ResturantReviews / Index / 2的url读取ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.