简体   繁体   English

ASP.Net MVC:PartialView的问题

[英]ASP.Net MVC: Problems with PartialView

I have LogOn.ascx control which is located on master page site.master: 我有位于主页Site.master上的LogOn.ascx控件:

<% Html.RenderPartial("LogOn"); %>

This control contains form with email and password textboxed which is submitted to LoginController: 此控件包含带有电子邮件和密码文本框的表单,该表单已提交到LoginController:

public class LoginController : Controller
{
     ...
    [HttpPost]
    public ActionResult Login(string email, string password)
    {
        if (this.userRepository.ExistByEmail(email) &&
            this.authenticationService.IsPasswordMatches(email, password))
        {
            var user = this.userRepository.GetByEmail(email);
            this.userSession.LogIn(user);
            return PartialView("LogOn", user);
        }
        return PartialView("LogOn");
    }
}

So if user is successfully authenticated I pass user into model of LogOn partial view (simplified): 因此,如果用户成功通过身份验证,则将用户传递到LogOn局部视图(简化)模型中:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Core.Model.User>" %>
<%= this.Model.FirstName %>

I have 2 problems with this code: 这段代码有2个问题:

  1. After calling return PartialView("LogOn") I get exception "The IControllerFactory 'UI.Services.ControllerFactory' did not return a controller for the name 'default.aspx'" This issue is solved by adding routing for "default.aspx". 调用return PartialView("LogOn")异常“ IControllerFactory'UI.Services.ControllerFactory'未返回名称为'default.aspx'的控制器”。通过为“ default.aspx”添加路由来解决此问题。 But why request goes to "default.aspx" when I call return PartialView(..) ? 但是,为什么在我调用return PartialView(..)时请求转到“ default.aspx”? (I'm using VS web server) (我正在使用VS Web服务器)

  2. I get null reference exception inside LogOn.ascx even if user was successfully authenticated and non-null value was passed into PartialView on the following line: 即使成功验证了用户身份,并且在以下行中将非空值传递给PartialView,我也仍然在LogOn.ascx中得到空引用异常:

Does anybody have an idea why user is not passed into LogOn.ascx? 有人知道为什么不将用户传递到LogOn.ascx吗? Thanks 谢谢

I assume that your login form is doing a standard submit back to the server (ie sending the whole page) rather than an AJAX post? 我假设您的登录表单正在将标准提交回服务器(即发送整个页面),而不是AJAX帖子?

If that's the case, then your action result from the Login action should be a whole view, not a partial - there's no page to host the partial in. 如果是这种情况,那么您从“登录”操作获得的操作结果应该是整体视图,而不是局部视图-没有页面可以容纳局部视图。

Following on from this as you've added a route for Default now, you're not actually passing the User object to the partial as you call it from the page: 在此之后,您现在已经为Default添加了一条路由,实际上并没有在从页面调用User对象时将其传递给Partial:

<% Html.RenderPartial("LogOn"); %>

You'd need to be calling this with the user, which would mean that you need to be adding the user to either the ViewData or the models that you're passing back if you're not using membership providers. 您需要与用户一起调用它,这意味着如果您不使用成员资格提供程序,则需要将用户添加到ViewData或要传递回的模型中。

I assume you're doing some checks in the LogOn partial to check if the request is authenticated other than checking to see if the User object has been passed in - because most of your calls to this partial will be from the master page which doesn't pass in the User object, these will mostly fail. 我假设您正在LogOn部分中进行一些检查,以检查请求是否已通过验证,而不是查看是否已传递User对象-因为您对该部分的大部分调用将来自母版页,而如果不传递User对象,这些操作大多数都会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM