部分视图页面中的NullReferenceException

时间:2012-06-11 19:02:48

标签: asp.net-mvc-3 asp.net-mvc-3-areas

感谢您阅读本文。

我不知道为什么这会在_GetDate.cshtml中抛出NullReferenceException:

<legend>For This @Model.lob.ToUpper() Please Enter Date Range</legend>

SomeController传递模型 - &gt; Index.cshtml - &gt; @ Html.Partial(“_ GetDate”,Model)

另外,当我在中间窗口中断开一行时,我可以看到@ Model.lob的值。

这是堆栈跟踪:

System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Web_l250s0ch
StackTrace:
    at ASP._Page_Views_Shared__GetDate_cshtml.Execute() in c:\Visual Studio 2010\Projects\Web\SomeProject\SomeProject\Views\Shared\_GetDate.cshtml:line 7
    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_Balance_GetDate_cshtml.Execute() in c:\Visual Studio 2010\Projects\Web\SomeProject\SomeProject\Views\Balance\GetDate.cshtml:line 22
    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.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
InnerException: 

这是GetDate.cshtml(第22行= @ Html.Partial(“_ GetDate”,Model)):

@model SomeProject.Models.DateParameter

@{
    ViewBag.Title = "GetDate";
}

@section script{
 @Content.Script("/UI/jquery.ui.core.js", Url)
 @Content.Script("/UI/jquery.ui.widget.js", Url)
 @Content.Script("/UI/jquery.ui.datepicker.js", Url)
 @Content.Script("MyCustomScript.js", Url)
}

@*<script src="@Url.Content("~/Scripts/UI/jquery-ui-1.8.18.custom.js")" type="text/javascript"></script>*@
@*<script src="@Url.Content("~/Scripts/UI/jquery.ui.core.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/UI/jquery.ui.widget.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/UI/jquery.ui.datepicker.js")" type="text/javascript"></script>*@
@*@Content.ScriptinUI("jquery.ui.datepicker.js", Url)*@

<div id="table_div">

    @Html.Partial("_GetDate", Model)

</div>

这是“_GetDate”:

@model SomeProject.Models.DateParameter
<h2>@Model.lob.ToUpper() </h2>
    <table id="MainTable">
    <tr class="DatePicker"> <td>
    @Model.lob.ToUpper() 
    </td></tr>
</table> 

以下是Controller的代码:

    public ActionResult GetDate(string lob)
    {
        var model = new DateParameter();
        model.lob = lob;
        ViewBag.lob = lob;
        return View(model);
    }

    [HttpPost]
    public ActionResult GetDate(FormCollection values, DateParameter newDateParameter)
    {
        if (ModelState.IsValid)
        {
            TempData["MyDate"] = newDateParameter;                
            return RedirectToAction("Listing");
        }
        else
        {
            return View(newDateParameter);
        }
    }

还可以包括DateParameter的类:

public class DateParameter : IValidatableObject
{
    [Required]
    [StringLength(3)]
    public virtual string lob { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    [DisplayName("Start Date")]
    public virtual DateTime DateStart { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    [DisplayName("End Date")]
    public virtual DateTime DateEnd { get; set; }

    //VALIDATE DATES

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        //SET ERROR NEXT TO THE FIELD
        var field = new[] { "DateEnd" };

        if (DateEnd > DateTime.Now.AddDays(1))
        {
            yield return new ValidationResult("The End Date Cannot Be Greater Than Today", field);
        }
    }
}

知道如何在局部视图页面中返回@ Model.lob中的字符串吗?

TIA!

2 个答案:

答案 0 :(得分:1)

重新启动的内容纠正了问题。希望这会有所帮助。

答案 1 :(得分:0)

@ Model.lob是否已设置?

如果没有,调用它上面的方法将抛出报告的异常。