Object reference not set to an instance of an object on layout page
我在编辑页中有一个编辑表单。当我第一次进入该页时,一切都正常。但提交表单后,我看到以下错误:
Object reference not set to an instance of an object. Description: An
unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
在源代码错误中,您可以看到:
1 2 3 4 | @{ ViewBag.Title ="Edit"; Layout ="~/Views/Shared/_Layout.cshtml"; } |
现在怎么了?!
更新
我的观点代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | @model Portal.Web.CMS.Models.PageViewModel @{ ViewBag.Title ="?????? ????"; Layout ="~/Views/Shared/_Layout.cshtml"; } ???? <ul class="breadcrumb" style="background-color: white;"> <li> ???? ????? <span class="divider">/</span> </li> <li> @Model.Name <span class="divider">/</span> </li> <li class="active">?????? ???? </li> </ul> @using (Html.BeginForm()) { @Html.ValidationSummary(true) @Html.HiddenFor(model => model.PageId) @Html.LabelFor(model => model.Lang) @if (Model.Lang.ToString() =="Fa") { <button type="button" value="2" class="btn">English</button> <button type="button" value="1" class="btn active">?????</button> } else { <button type="button" value="2" class="btn active">English</button> <button type="button" value="1" class="btn">?????</button> } @Html.Hidden("Lang") @Html.LabelFor(model => model.Name) @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) @Html.LabelFor(model => model.Url) @Html.EditorFor(model => model.Url) @Html.ValidationMessageFor(model => model.Url) @Html.LabelFor(model => model.Title) @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) @Html.LabelFor(model => model.PageContent) @Html.TextAreaFor(model => model.PageContent) @Html.ValidationMessageFor(model => model.PageContent) <p> <button type="button" id="addFile" class="btn btn success">???? ?????</button> </p> <p> <input type="file" name="files" /> </p> <p> <button type="button" id="addPicture" class="btn btn success">??? ?????</button> </p> <p> <input type="file" name="images" /> </p> if (Model.PageAttachements!=null ) { <fieldset> <legend>???? ??? ?????</legend> @foreach (var file in Model.PageAttachements) { × @file.Name } </fieldset> } if (Model.PageImages.Count > 0) { <fieldset> <legend>?????? ?????</legend> @foreach (var image in Model.PageImages) { × <img src="~/Images/@image.Name" /> } </fieldset> } <p> <input type="submit" value="?????" class="btn btn-primary btn-large" /> </p> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") } |
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public ActionResult Edit(int id) { var model = Mapper.Map<PageViewModel>(pageApp.GetPageById(id)); return View(model); } [HttpPost] public ActionResult Edit(PageViewModel model) { bool result = false; if (model.Lang.ToString() =="Fa") result = pageApp.IsRepetitive(model.Url, 1); else result = pageApp.IsRepetitive(model.Url, 2); if (result == true) { TempData["error"] ="???? ?? ?? ???? ???? ? ???? ???? ????."; return View(); } else { if (model.PageContent != null) { var page = Mapper.Map<Page>(model); page.UserId = Portal.Web.CMS.Components.SessionContext.GetUserData().UserId; page.PageDate = DateTime.Now; pageApp.Update(page); TempData["success"] ="???? ?? ?????? ????? ????."; return RedirectToAction("Details", new { url = model.Url }); } else { TempData["error"] ="????? ???.???? ???? ?? ???? ???."; return View(); } } } |
在这些线路中:
1 2 | TempData["error"] ="????? ???.???? ???? ?? ???? ???."; return View(); |
您返回的是没有模型的
您需要将适当的模型返回到视图:
1 | return View(model); |
旁注:既然您使用的是强类型模型,那么您可能希望在模型中添加一个
因为返回视图();没有模型对象正如您在编辑类返回视图(模型)中给出的那样