Reputation: 749
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:
Upvotes: 1
Views: 916
Reputation: 749
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.
Upvotes: 2
Reputation: 4146
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.
Upvotes: 0
Reputation: 1038800
public ViewResult Test()
{
return View(new TestModel());
}
Upvotes: 0
Reputation: 5719
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.
Upvotes: 0