参数词典共包含参数无效项非可空类型的“restaurantId'System.Int32 [英] The parameters dictionary contains a null entry for parameter 'restaurantId' of non-nullable type 'System.Int32

查看:543
本文介绍了参数词典共包含参数无效项非可空类型的“restaurantId'System.Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进出口新的堆栈overflow.I开始创建MVC应用程序,并而是采用了Map.Route我使用绑定属性在id通过。下面显示的错误和code为每个类/组件。


  

参数词典共包含参数无效项
  restaurantId非可空类型'System.Int32'对方法
  System.Web.Mvc.ActionResult指数(Int32)已在
  OdeToFood.Controllers.ResturantReviewsController。可选
  参数必须是引用类型,可空类型,或声明为
  一个可选的参数。


示范

 公共类ResturantReview
{
    公众诠释标识{搞定;组; }
    公众诠释评级{搞定;组; }
    公共字符串车身{搞定;组; }
    公共字符串ReviewerName {搞定;组; }
    公众诠释ResturantId {搞定;组; }}调节器
公共类ResturantReviewsController:控制器
    {
        私人OdeToFoodDb _db =新OdeToFoodDb();        公众的ActionResult指数([装订(preFIX =ID)INT restaurantId)
        {
            VAR餐厅= _db.Resturants.Find(restaurantId);
            如果(餐厅!= NULL)
            {
                返回查看(餐厅);
            }
            返回HttpNotFound();
        }        [HTTPGET]
        公众的ActionResult创建(INT restaurantId)
        {
            返回查看();
        }        [HttpPost]
        公众的ActionResult创建(ResturantReview综述)
        {
            如果(ModelState.IsValid)
            {
                _db.Reviews.Add(审查);
                _db.SaveChanges();
                返回RedirectToAction(指数,新{ID = review.ResturantId});
            }
            返回查看(审查);
        }        保护覆盖无效的Dispose(BOOL处置)
        {
            如果(处置)
            {
                _db.Dispose();
            }
            base.Dispose(处置);
        }

查看

  @model OdeToFood.Models.Resturant@ {
    ViewBag.Title =指数;
}< H2>指数< / H>
@ Html.Partial(查看,Model.Reviews)
&所述p为H.;
    @ Html.ActionLink(新建,创建,新{restaurantId = Model.Id})
&所述; / P>


解决方案

这是什么看你有没有采取行动。

 公众的ActionResult指数([装订(preFIX =ID)INT restaurantId)

删除绑定属性

 公众的ActionResult指数(INT ID)

绑定preFIX意味着你POST /获取参数作为id.restaurantId。你想赶上默认的路由,将来自像URL中读取ID ResturantReviews /索引/ 2

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. 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'. 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. And you want to catch default routing which will read id from url like ResturantReviews/Index/2

这篇关于参数词典共包含参数无效项非可空类型的“restaurantId'System.Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆