I know this sounds like every other question on the net about this but it's actually not. I have tried finding the right answer high and low so as to not waste anyone's time but to no avail. I should also add that I am pretty new to MVC.NET.
I have an MVC 4 view with a DropDownListFor call that throws a null ref exception on post. I am trying to test a submit with nothing selected scenario i.e. the default selection is selected. Ideally, it will get picked up and yell at me with a required field message. I see that the Province model property is in fact set to -1 on the post, so that works.
Now this is where my question deviates from most others. I am pretty sure that the model is being passed correctly and that the SelectList is populated. I have set a breakpoint on the line in the view and am seeing it populated on the post just before it blows up. My code seems to look like every other example I have seen.
I very much appreciate any help you can provide.
At the end, I will paste the yellow screen info.
So here are the snippets, I cut out the bulk of it so you're not overwhelmed with non-relevant code:
View:
@using GymManagement.UI.Models
@model UserModel
@Html.DropDownListFor(m => m.Province, Model.ProvinceList, new {@id="ProvincePersonal", @class="inputField", @value="@Model.Province"})
Controller:
public ActionResult CreateMember()
{
return CreateUser();
}
[HttpPost]
public ActionResult CreateMember(UserModel model)
{
return CreateUser(model);
}
private ActionResult CreateUser()
{
var model = new UserModel();
PrepareModel(model, false);
return View(model);
}
private ActionResult CreateUser(UserModel model)
{
if (ModelState.IsValid)
{
return DisplayUser(model);
}
PrepareModel(model, true);
return View(model);
}
private void PrepareModel(UserModel model, bool isPostback)
{
// other items removed for brevity
if (Session["Provinces"] == null || ((List<Province>)Session["Provinces"]).Count == 0)
{
var serviceClient = ServiceProxy.GetLookupService();
var provinces = serviceClient.GetProvinces(); // Returns List<Province>
provinces = provinces.OrderBy(p => p.ProvinceName).ToList();
Session["Provinces"] = provinces;
model.Provinces = provinces;
}
else
{
model.Provinces = ((List<Province>)Session["Provinces"]);
}
}
Model:
// base model
public class BaseModel
{
public BaseModel()
{
Provinces = new List<Province>();
}
public List<Province> Provinces { get; set; }
}
// user model
public int Province { get; set; }
public IEnumerable<SelectListItem> ProvinceList
{
get
{
var list = new SelectList(Provinces, "ProvinceId", "ProvinceName");
var defaultItem = Enumerable.Repeat(new SelectListItem
{
Value = "-1",
Text = "Select province"
}, count: 1);
defaultItem = defaultItem.Concat(list);
if (Province != 0)
{
var selectedItem = Province.ToString();
var province = defaultItem.First(p => p.Value.Equals(selectedItem));
province.Selected = true;
}
return defaultItem;
}
}
Object reference not set to an instance of an object.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] ASP._Page_Views_user_CreateMember_cshtml.Execute() in c:\Users\Mike\documents\visual studio 2012\Projects\GymManagement\GymManagement.UI\Views\User\CreateMember.cshtml:46 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +279 System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +124 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +180 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +379 System.Web.Mvc.<>c__DisplayClass1a.b__17() +32 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func
1 continuation) +613 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1 filters, ActionResult actionResult) +263 System.Web.Mvc.Async.<>c__DisplayClass25.b__22(IAsyncResult asyncResult) +240 System.Web.Mvc.<>c__DisplayClass1d.b__18(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.Mvc.<>c__DisplayClass8.b__3(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
-1
is valid for typeofint
. Not really sure what your trying to achieve with all this code. It can be reduced down to just a few lines. - user3559349