Html.BeginForm - 未将对象引用设置为对象的实例

发布于 2024-11-02 17:43:56 字数 1952 浏览 0 评论 0原文

我有一个 MVC2 ASP.Net 4 应用程序,它有一个登录页面,该页面使用应用程序母版页的布局。 “默认”登录页面是典型的:

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

现在我必须显示具有完全不同布局的登录页面,但我希望它执行与“默认”登录视图相同的操作,即调用相同的控制器操作并使用相同的网络模型。

他们希望用户访问 www.mydomain.com/alternateLogOn.aspx

所以我在我的 global.asax 中这样做:

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

这让我得到了我想要显示的页面,没有母版页布局(不包括 MasterPageFile=) :

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

页面在不使用(Html.BeginForm)的情况下显示良好,但是当我使用它时,我收到对象引用异常。

这是堆栈跟踪:

堆栈跟踪:

[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

我尝试添加 <%@ Import namespace="System.Web.Mvc.Html" %> 但它没有帮助

谢谢...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

完美的未来在梦里 2024-11-09 17:43:56

将登录表单放入局部视图中可能是最简单的方法。然后,创建两个不同的页面,每个页面调用不同的母版页/布局。在每个页面中,调用 Html.RenderPartial("LogonForm")。

(在 MVC 中通常不需要 URL 重写。 使用路线 代替。)

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.)

空城之時有危險 2024-11-09 17:43:56

这里发生了两件事之一:Html.RouteCollection 为 null,或者 Html.ViewContext 为 null。

我在这里不是 100% 确定,但看起来 ASP.NET 正在处理此请求,而不是 MVC,因此无法填充这些值。这是有道理的,因为您将用户直接重定向到 aspx 页面。

如果您只想更改页面的布局,请将其实现为自己的操作/视图并使用 此重载用于View(),它将母版页作为参数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文