为什么只有一个动作看不到HTMLHelper?

时间:2017-08-18 19:14:47

标签: c# asp.net-mvc

我试图制作一个简单的asp.net-mvc应用程序。我已经制作了我的模型类并将其添加到数据库中。我使用内置方法使用Entity Framework生成带有视图的控制器。但出于某种原因,当我尝试查看Create页面(并且只有Create页面,而不是编辑页面!)时,我得到一个Null Reference Exception(对象引用未设置为对象的实例)。

所以我的创建行动:

    public ActionResult Create()
    {
        return View();
    }

这是我的编辑操作:

    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        SupplierCorrection supplierCorrection = db.SupplierCorrectionSet.Find(id);
        if (supplierCorrection == null)
        {
            return HttpNotFound();
        }
        return View(supplierCorrection);
    }

这是我的创建页面:

@model FAIR.Data.Models.SupplierCorrection

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>SupplierCorrection</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.Partial("_createEdit", Model)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section scripts{
    <script type="text/javascript" src="~/Scripts/SupplierCorrectionsCreateEdit.js"></script>
}

这是我的编辑页面(精明的读者会注意到它非常相似):

@model FAIR.Data.Models.SupplierCorrection

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>SupplierCorrection</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.ID)

        @Html.Partial("_createEdit", Model)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section scripts{
    <script type="text/javascript" src="~/Scripts/SupplierCorrectionsCreateEdit.js"></script>
}

和这里的_createEdit:

@model FAIR.Data.Models.SupplierCorrection

<div class="form-group">
    @Html.LabelFor(model => model.FAIRNumber, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.FAIRNumber, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.FAIRNumber, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Supplier, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Supplier, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Supplier, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Closed, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <div class="checkbox">
            @Html.EditorFor(model => model.Closed)
            @Html.ValidationMessageFor(model => model.Closed, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

<div class="form-group">
    <label class="control-label col-md-2">Corrections Link</label>
    <div class="col-md-10">
        <input type="file" id="CorrectionsLinkFilePicker" onchange="CorrectionsLinkChosen();" />
        <input type="hidden" name="CorrectionsLink" id="CorrectionsLink" value="@Model.CorrectionsLink" />
        @if (!string.IsNullOrWhiteSpace(Model?.CorrectionsLink))
        {
            @:Currently: <a href="@Model.CorrectionsLink">@Model.CorrectionsLink</a>
        }
        @Html.ValidationMessageFor(model => model.CorrectionsLink, "", new { @class = "text-danger" })
    </div>
</div>

当我导航到“创建”页面(再次,而不是编辑页面)时,它在第24行出现错误,我在那里Html.ValidationMesageFor(model => model.Closed, ...但是如果我删除它,则会在上一行中出错。依此类推,直到我using(Html.BeginForm())为止。我想作为权宜之计,我可以在不使用Html对象的情况下重写页面,但如果再次发生这种情况(或在较长的页面上),我想知道为什么它在世界上突然去了。

完整堆栈跟踪:

   at ASP._Page_Views_SupplierCorrections__createEdit_cshtml.Execute() in C:\Users\ttm920632\Desktop\Repos\FAIR\FAIR\Views\SupplierCorrections\_createEdit.cshtml:line 24
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
   at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model)
   at ASP._Page_Views_SupplierCorrections_Create_cshtml.Execute() in C:\Users\ttm920632\Desktop\Repos\FAIR\FAIR\Views\SupplierCorrections\Create.cshtml:line 18
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

0 个答案:

没有答案