1

I'm seeing and odd exception and completely out of ideas on how to resolve this. I'm sure I'm overlooking something very obvious. I had the following view work but did something and now cannot figure out why it suddenly stopped.

// All in an Area

// Controller
public ViewResult Test()
{
    return View();
}

[HttpPost]
public ViewResult Test(TestModel testModel)
{
    // Do some work
    // Redirect or
    return View(testModel);
}

// Model
public class TestModel
{
    [Required]
    public string Name { get; set; }
}

// View
@model MyApp.Areas.Admin.Models.TestModel

@{
    ViewBag.Title = "Create New User";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section HeadSection {
    <link href="@Url.Content("~/Content/MvcMembership.css")" rel="stylesheet" type="text/css" />
}

<h2 class="someclass">Test Form</h2>

<div class="otherclass">
    @using (Html.BeginForm("Test", "MyController"))
    { 
    @Html.EditorForModel()
        <input type="submit" value="Create" />
        @Html.ValidationSummary(true)
} 
</div>

I receive the following exception at @Html.EditorForModel() on the HttpGet in my actual code or in tests controllers/views.

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=App_Web_field.master.5f132152.av8tutrk
  StackTrace:
       at ASP.views_inputbuilders_editortemplates_field_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in http://server/Views/InputBuilders/EditorTemplates/Field.Master:line 5
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

Any thoughts would be GREAT!!! -- Jeff

[Edit] Few bullets:

  • I have a working solution/project with test code. The major difference is project size. This is a big project with lots of areas, controllers, models and views. So routing, references, etc... are different. I have even stepped through and confirmed Model is null and with what little reading I can find it appears EditorFor[Model] uses reflections to create fields so it is something else.
  • I found a working example online while searching for solution: http://mvcmembership.codeplex.com/
2
  • a: did you pass in a non-null TestModel as the model? b: what editor is it using? a default one? a custom one? what? Commented Nov 7, 2011 at 6:11
  • Both non-null and null. TestModel is the model. I'm using VS2010. See my question edits above. Commented Nov 7, 2011 at 13:06

4 Answers 4

2

Turns out I had removed a portable area using MvcContrib.Mvc3-ci (v3.0.90.0) Nuget Package however did not remove usages in the Global.asax.cs file in the Application_Start(). In short I removed MvcContrib altogether and it works fine. If I add the package and the Global.asax.cs usage and the issue returns. When I have a moment I'll do a bit more to understand the issue, write a blog with the details and submit issues if necessary.

With the MvcContrib.Mvc3-ci (v3.0.90.0) installed, the Global.asac.cs file contained:

using MvcContrib.PortableAreas; // REMOVED THIS!

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);

    PortableAreaRegistration.RegisterEmbeddedViewEngine(); // REMOVED THIS!
    RegisterRoutes(RouteTable.Routes);
}

Thank to Maess, Darin and Michal for your help. Can't mark any answers as correct but at the very least I can extend this thanks.

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

Comments

0

You use EditorForModel and you pass a null reference to it. Fix it by sending e.g. a new TestModel object to the view from the controller.

1 Comment

Thanks Michal, I did find the solution which I highlighted at the bottom of the question. I was sure it had to do with the project so might not have been a fair question. Best -- Jeff
0
public ViewResult Test()
{
    return View(new TestModel());
}

2 Comments

That does not solve the issue. I've even stepped through it and I have no idea what is null. I have a new solution and new project that works with exact same code. I also found in searching for solution other similar code which works. See question edits.
Thanks Darin, I did find the solution which I highlighted at the bottom of the question. I was sure it had to do with the project so might not have been a fair question. Best -- Jeff
0

Just a thought, but, if you are sending in a new TestModel, I bet the Name property is null, try using a member which is always set to at least string.Empty to hold the Name property and see if that resolves your issue.

1 Comment

Thanks Maess, I did find the solution which I highlighted at the bottom of the question. I was sure it had to do with the project so might not have been a fair question. Best -- Jeff

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.