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>
restaurantId
(notid
) so itsreturn RedirectToAction("Index", new { restaurantId = review.ResturantId });
and remove the[Bind(Prefix = "id")]
– user3559349