ASP.Net MVC:PartialView 的问题
我有位于母版页 site.master 上的 LogOn.ascx 控件:
<% Html.RenderPartial("LogOn"); %>
该控件包含带有电子邮件和密码文本框的表单,该表单被提交到 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");
}
}
因此,如果用户成功通过身份验证,我会将用户传递到 LogOn 部分视图的模型(简化):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Core.Model.User>" %>
<%= this.Model.FirstName %>
我对这段代码有两个问题:
调用
return PartialView("LogOn")
后,我收到异常“IControllerFactory 'UI.Services.ControllerFactory' 没有返回名称为 'default' 的控制器.aspx'" 通过为“default.aspx”添加路由可以解决此问题。但是当我调用return PartialView(..)
时,为什么请求会转到“default.aspx”? (我正在使用 VS Web 服务器)即使用户已成功通过身份验证并且非空值已传递到下一行的 PartialView 中,我也会在 LogOn.ascx 中得到空引用异常:
有人知道为什么用户没有传递到 LogOn.ascx 中吗? 谢谢
I have LogOn.ascx control which is located on master page site.master:
<% Html.RenderPartial("LogOn"); %>
This control contains form with email and password textboxed which is submitted to 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):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Core.Model.User>" %>
<%= this.Model.FirstName %>
I have 2 problems with this code:
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". But why request goes to "default.aspx" when I callreturn PartialView(..)
? (I'm using VS web server)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:
Does anybody have an idea why user is not passed into LogOn.ascx?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的登录表单正在执行标准提交回服务器(即发送整个页面)而不是 AJAX 发布?
如果是这种情况,那么登录操作的操作结果应该是整个视图,而不是部分视图 - 没有页面可以托管部分视图。
接下来,您现在已经添加了默认路由,您将当您从页面调用它时,实际上并未将 User 对象传递给部分:
您需要与用户一起调用它,这意味着您需要将用户添加到 ViewData 或您的模型中如果您不使用会员提供商,请返回。
我假设您正在 LogOn 部分中进行一些检查,以检查请求是否经过身份验证,而不是检查 User 对象是否已传入 - 因为对此部分的大多数调用将来自母版页,而母版页不会如果不传入 User 对象,这些大多会失败。
I assume that your login form is doing a standard submit back to the server (i.e. sending the whole page) rather than an AJAX post?
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:
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.
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.