Thanks for reading this.
I have no idea why this is throwing a NullReferenceException in _GetDate.cshtml:
<legend>For This @Model.lob.ToUpper() Please Enter Date Range</legend>
SomeController pass model --> Index.cshtml --> @Html.Partial("_GetDate", Model)
Also, when I break one line above in the intermediate Window I could see the value for @Model.lob.
Here's the stack trace:
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:
Here's GetDate.cshtml (line 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>
Here's "_GetDate":
@model SomeProject.Models.DateParameter
<h2>@Model.lob.ToUpper() </h2>
<table id="MainTable">
<tr class="DatePicker"> <td>
@Model.lob.ToUpper()
</td></tr>
</table>
Here's code from the 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);
}
}
Might as well include the class for 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);
}
}
}
Any idea how to return the string in @Model.lob in the partial view page?
TIA!
Modelyou're passing in isnullor.caseisnullcaseis a reserved word in C#. It's an extremely poor naming choice for a property. The convention in C# dictates that property names must start with a capital letter.GetDate. Perhaps the controller doesn't know which action to call when passing more than one variable and calls them both. Try putting a breakpoint in both actions and see if they both get hit with one post. The action decorated with theHttpPostattribute could give you an invalid.lob, because it is not assigned there. It would explain a flashing line because there are two threads parsing your view and only one has the proper value assigned. Pressing F11 is probably jumping from thread to thread.