繁体   English   中英

参数字典包含非空类型'System.Int32的参数'restaurantId'的空条目

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM