我是MVC 4的新手。
我收到以下错误:
Object reference not set to an instance of an object. Line 47:@Html.ActionLink("Back to List", "Index", new { id = Model.RestaurantId })
以下是导致错误的观点(请参阅最后几行):
@model OdeToFood.Models.RestaurantReview
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>New Review</legend>
<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.Body)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Body)
@Html.ValidationMessageFor(model => model.Body)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ReviewerName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReviewerName)
@Html.ValidationMessageFor(model => model.ReviewerName)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index", new { id = Model.RestaurantId })
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
这是我在单击“返回列表”链接时调用的控制器操作:
public ActionResult Index([Bind(Prefix="id")]int restaurantId)
{
var restaurant = _db.Restaurants.Find(restaurantId);
if (restaurant != null) {
return View(restaurant);
}
return HttpNotFound();
}
以下是在视图中强烈输入的RestaurantReview模型:
public class RestaurantReview
{
public int Id { get; set; }
public int Rating { get; set; }
public string Body { get; set; }
public string ReviewerName { get; set; }
public int RestaurantId { get; set; }
}
非常感谢任何帮助。
干杯!
答案 0 :(得分:0)
在您的代码中,您显示的是Create
视图和Index
控制器。检查Create
控制器中是否通过Model
,或检查是否需要型号。