I am getting the ubiquitous "object reference" error and don't know how to resolve it. I believe it has to do with calling a partial view. I am using a jquery wizard, so the partial views are the "steps" in the wizard that get displayed.
In my main .cshtml
view I do this (I'm leaving out the HTML):
@using MyNamespace.Models
@using MyNamespace.ViewModels
@model MyViewModel
...
...
using (Html.BeginForm())
{
...
// this works inside MAIN view (at least it goes through
// before I get my error)
if (Model.MyModel.MyDropDown == DropDownChoice.One)
{
//display something
}
...
// here i call a partial view, and in the partial view (see
// below) I get the error
@{ Html.RenderPartial("_MyPartialView"); }
...
}
The above works (at least it goes through before I get to my error).
Here is my partial view (again, leaving out HTML):
@using MyNamespace.Models
@using MyNamespace.ViewModels
@model MyViewModel
....
// I get the object reference error here
@if (Model.MyModel.MyRadioButton == RadioButtonChoice.One)
{
// display something
}
....
I am confused because with the exception of the @if
vs. if
, it's essentially the same code. I do not know what I've done wrong, or how to fix.
For context, here is MyViewModel
:
public class MyViewModel
{
public MyModel MyModel { get; set; }
}
And the MyDropDown
and MyRadioButton
are using enums
thusly:
public enum DropDownChoice { One, Two, Three }
public enum RadioButtonChoice { One, Two, Three }
public DropDownChoice? MyDropDown { get; set; }
public RadioButtonChoice? MyRadioButton { get; set; }
My controller only has an action for the main form and no actions for the partial view:
public ActionResult Form()
{
return View("Form");
}
[HttpPost]
public ActionResult Form(MyViewModel model)
{
if (ModelState.IsValid)
{
return View("Submitted", model);
}
return View("Form", model);
}
Any thoughts? Is it that I have to create an ActionResult
for that partial view even though there is no direct call to it (other than as a partial view in the wizard)? Thank you.