I am trying to pass an object of model class for edit but receing this error “The model item passed into the dictionary is of type ‘System.Collections.Generic.List`1[MvcApplication1.Models.ResturantReviews]’, but this dictionary requires a model item of type ‘MvcApplication1.Models.ResturantReviews’.”
Kindly guide me where I am wrong, here is my code:
My Model class:
namespace MvcApplication1.Models{
public class ResturantReviews
{
public int ReviewId { get; set; }
public int Rating { get; set; }
public string ReviewBody { get; set; }
public Resturant Resturant { get; set; }
}
}
My Action code:
public ActionResult Edit(int Id)
{
//var review = FoodDB.review;
ResturantReviews obj = new ResturantReviews {
ReviewId = 2,
Rating = 9,
ReviewBody = "Test Sarina is a good resturant for continantal food",
Resturant = new Resturant
{
ResturantId = 1,
ResturantAddress = "Commercial area",
ResturantName = "Sarina Hotel"
}
};
return View(obj);
}
My code in View:
@model MvcApplication1.Models.ResturantReviews
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ResturantReviews</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ReviewId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReviewId)
@Html.ValidationMessageFor(model => model.ReviewId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rating)
@Html.ValidationMessageFor(model => model.Rating)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ReviewBody)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReviewBody)
@Html.ValidationMessageFor(model => model.ReviewBody)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
The Edit controller action (as shown in your question) passes a single instance of
ResturantReviewsand the view is strongly typed to@model ResturantReviewsso this should not throw an exception. The code seems correct as is.So you could try narrowing down by simplifying at maximum:
and the
Edit.cshtmlview:This should work 100%. Then you could start adding functionality.