对象引用未设置为布局页面上对象的实例

Ham*_*eza 0 c# asp.net-mvc-4

我在编辑页面中有一个编辑表单.当我第一次进入页面时一切正常.但是在提交表单后我看到了这个错误:

你调用的对象是空的.描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例.

在源错误中你看到了这个:

@{
    ViewBag.Title = "Edit";
    Layout ="~/Views/Shared/_Layout.cshtml";
}
Run Code Online (Sandbox Code Playgroud)

现在有什么问题?!

更新

我的观看代码:

@model Portal.Web.CMS.Models.PageViewModel

@{
    ViewBag.Title = "?????? ????";
    Layout ="~/Views/Shared/_Layout.cshtml";
}

<h3>????</h3>
<ul class="breadcrumb" style="background-color: white;">
    <li><a href="~/Page">???? ?????</a> <span class="divider">/</span></li>
    <li><a href="@Url.Action("Details", "Page", new { url = Model.Url })">@Model.Name</a> <span class="divider">/</span></li>
    <li class="active">?????? ????</li>
</ul>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    @Html.HiddenFor(model => model.PageId)
    <div class="editor-label">
        @Html.LabelFor(model => model.Lang)
    </div>
    <div class="editor-field">
        <div class="btn-group" data-toggle="buttons-radio" dir="ltr">
            @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>
            }
        </div>
        @Html.Hidden("Lang")
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Url)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Url)
        @Html.ValidationMessageFor(model => model.Url)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PageContent)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.PageContent)
        @Html.ValidationMessageFor(model => model.PageContent)
    </div>
    <div id="attachments" class="editor-field">
        <p>
            <button type="button" id="addFile" class="btn btn success">???? ?????</button>
        </p>
        <p>
            <input type="file" name="files" />
        </p>
    </div>
    <div id="productImages" class="editor-field">
        <p>
            <button type="button" id="addPicture" class="btn btn success">??? ?????</button>
        </p>
        <p>
            <input type="file" name="images" />
        </p>
    </div>
    if (Model.PageAttachements!=null )
    {
    <fieldset>
        <legend>???? ??? ?????</legend>
        <div class="row-fluid show-grid">
            @foreach (var file in Model.PageAttachements)
            {
                <div class="span4 thumbnail">
                    <a href="~/Page/DeleteFile/@file.PageAttachmentId" class="close pull-left" onclick="return confirm('??? ???????? ???? ??? ?? ??? ???????')" >&times;</a>
                    @file.Name
                </div>
            }
        </div>
    </fieldset>
    }
    if (Model.PageImages.Count > 0)
    {
    <fieldset>
        <legend>?????? ?????</legend>
        <div class="row-fluid show-grid">
            @foreach (var image in Model.PageImages)
            {
                <div class="span4 thumbnail">
                    <a href="~/Product/DeleteImage/@image.PageImageId" class="close pull-left" onclick="return confirm('??? ???????? ???? ??? ?? ??? ???????')" >&times;</a>
                    <img src="~/Images/@image.Name" />
                </div>
            }
        </div>
    </fieldset>
    }
    <p>
        <input type="submit" value="?????" class="btn btn-primary btn-large" />
    </p>
}


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
Run Code Online (Sandbox Code Playgroud)

和控制器:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*gue 8

在这些方面:

TempData["error"] = "????? ???.???? ???? ?? ???? ???.";
return View();
Run Code Online (Sandbox Code Playgroud)

您将返回Edit没有模型的视图.因此,当您尝试在视图中访问模型时,会出现NullReferenceException.

您需要将相应的模型返回到视图:

return View(model);
Run Code Online (Sandbox Code Playgroud)

旁注:由于您仍在使用强类型模型,因此您可能希望Error在模型中添加属性而不是使用TempData.