感谢您阅读本文。
我不知道为什么会在 _GetDate.cshtml 中抛出 NullReferenceException:
<legend>For This @Model.lob.ToUpper() Please Enter Date Range</legend>
SomeController 传递模型 --> Index.cshtml --> @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>
这是来自控制器的代码:
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 中的字符串吗?
蒂亚!