1

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!

18
  • Can you show the stack trace and relevant code? Commented Jun 11, 2012 at 19:05
  • 1
    I'm sure that the Model you're passing in is null or .case is null Commented Jun 11, 2012 at 19:05
  • 2
    off-topic: case is 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. Commented Jun 11, 2012 at 19:07
  • 2
    Comments are not supposed to be used to post code snippets. Please update your question to include this information and remove it from the comments. Commented Jun 11, 2012 at 19:22
  • 1
    The problem you describe could occur because you have two actions with the same name 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 the HttpPost attribute 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. Commented Jun 11, 2012 at 23:50

2 Answers 2

1

For what it's worth rebooting corrected the issue. Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Is the @Model.lob set?

If not, calling a method on it will throw the reported exception.

1 Comment

hi, yes it's set from the querystring. Thanks for asking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.