简体   繁体   English

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

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

相关问题 参数字典包含非可空类型&#39;System.Int32&#39;的参数&#39;idd&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'idd' of non-nullable type 'System.Int32' MVC:参数字典包含非可空类型'System.Int32'的参数'k'的空条目 - MVC : The parameters dictionary contains a null entry for parameter 'k' of non-nullable type 'System.Int32' 参数字典包含非空类型&#39;System.Int32&#39;的参数&#39;SolutionArchitectID&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'SolutionArchitectID' of non-nullable type 'System.Int32' 参数字典包含非空类型&#39;System.Int32&#39;的参数&#39;id&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' 参数字典包含非空类型“ System.Int32”的参数“ imageWidth”的空条目 - The parameters dictionary contains a null entry for parameter 'imageWidth' of non-nullable type 'System.Int32' 参数字典包含用于方法的非空类型&#39;System.Int32&#39;的参数&#39;userId&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for method 参数字典包含非可空类型&#39;System.Int32&#39;的参数&#39;testId&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'testId' of non-nullable type 'System.Int32' 参数字典包含用于方法的非空类型&#39;System.Int32&#39;的参数&#39;foo&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'foo' of non-nullable type 'System.Int32' for method 参数字典包含非空类型&#39;System.Int32&#39;的参数&#39;_id&#39;的空条目 - The parameters dictionary contains a null entry for parameter '_id' of non-nullable type 'System.Int32' 参数字典包含非空类型“ System.Int32”的参数“ did”的空条目 - The parameters dictionary contains a null entry for parameter 'did' of non-nullable type 'System.Int32'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM