tom
tom

Reputation: 63

Html.BeginForm - Object reference not set to an instance of an object

I have an MVC2 ASP.Net 4 app that has a log on page which uses the layout from the app's master page. The 'default' log on page is typical:

                    using (Html.BeginForm("LogIn", "Home", FormMethod.Post, new { id = "LogIn" }))
                    { ....form stuff...}

Now I have to show a log on page with a totally different layout but I want it to do the same thing(s) as the 'default' log on view, i.e. call the same controller action and use the same web model.

They want the users to go to www.mydomain.com/alternateLogOn.aspx

So I'm doing this in my global.asax:

  protected void Application_BeginRequest(object sender, EventArgs arg)
  {  if (Request.Url.PathAndQuery.ToLower() == "/AlternateLogOn.aspx")
     {  Context.RewritePath("/Views/Home/AlternateLogOn.aspx");
     }
  }

This gets me the page I want to show w/o the master page layout (not including MasterPageFile=):

<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage<MySite.Web.Models.AccountLogIn>" %>

The page displays fine w/o the using (Html.BeginForm), but when I use it I get the Object Reference exception.

Here's the stack trace:

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.Html.FormExtensions.BeginForm(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary`2 htmlAttributes) +42
System.Web.Mvc.Html.FormExtensions.BeginForm(HtmlHelper htmlHelper, String actionName, String controllerName, FormMethod method, Object htmlAttributes) +214
ASP.views_home_alternatelogon_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\Visual Studio 2010\MySite\MySite.Web\Views\Home\AlternateLogon.aspx:32
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +130
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +84
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5273

I tried adding <%@ Import namespace="System.Web.Mvc.Html" %> but it does not help

Thanks...

Upvotes: 1

Views: 2875

Answers (2)

Matt Sherman
Matt Sherman

Reputation: 8358

It is probably easiest to put the logon form into a partial view. Then, create two distinct pages, each of which calls a different master page/layout. Within each of those pages, call Html.RenderPartial("LogonForm").

(URL rewriting is generally unnecessary in MVC. Use routes instead.)

Upvotes: 1

Nick Larsen
Nick Larsen

Reputation: 18877

One of two things has happened here: either Html.RouteCollection is null, or Html.ViewContext is null.

I'm not 100% certain here, but it looks like the ASP.NET is handling this request, not MVC, hence the failure to populate those values. This would make sense since you are redirecting your user directly to an aspx page.

If all you want to do is change the layout of the page, implement it as it's own action/view and use this overload for View() which takes a master page as an argument.

Upvotes: 0

Related Questions