【问题标题】:Partial View with empty page ASP MVC 3带有空页面的部分视图 ASP MVC 3
【发布时间】:2013-05-10 08:38:32
【问题描述】:

我正在使用 C# 和 SQL Server 2005 开发一个 ASP.Net MVC 3 应用程序。我正在使用具有代码优先方法的实体框架。

我有一个 LOG ON(连接)接口,它与我的基地相关,我有一个 USER 表(包含登录名 + 密码)。

这是连接的视图:LogonPartial.acx(从 UserViewModel 强类型化的部分视图)

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.ViewModels.UserViewModel>" %>


<%
    if (Request.IsAuthenticated) {
%>

        Welcome <strong><%: Page.User.Identity.Name %></strong>!
        [ <%: Html.ActionLink("Log Off", "LogOff", "Account") %> ]
<%
    }
    else {
%> 
        [ <%: Html.ActionLink("Log On", "LogOn", "Account") %> ]
<%
    }
%>

连接成功时:我只有“登录”链接。 连接失败时:页面为空

这是控制器:

[ChildActionOnly]
        public ActionResult LogedInUser()
        {
            var user = new UserViewModel();
            if (Request.IsAuthenticated)
            {
                user.Nom_User = User.Identity.Name;
            }
            return PartialView(user);
        }
private GammeContext db = new GammeContext();
        [AcceptVerbs(HttpVerbs.Post)]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
            Justification = "Needs to take same parameter type as Controller.Redirect()")]
        public ActionResult LogedInUser(string Matricule, string passWord, bool rememberMe, string returnUrl)
        {
            if (!ValidateLogOn(Matricule, passWord))
            {
                return Connection(Matricule, passWord, returnUrl);
            }

            //FormsAuth.SignIn(Matricule, rememberMe);

            if (!String.IsNullOrEmpty(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        public ActionResult Connection(string Matricule, string passWord, string returnUrl)
        {
            List<User> users = db.Users.ToList();
            ActionResult output = null;

            if (users.Any())
            {
                foreach (User u in users)
                {
                    if ((u.Matricule == Matricule) && (u.passWord == passWord))
                    {
                        output = View();
                    }
                }
            }
            else
            {
                output = Redirect(returnUrl);
            }

            return output;
        }

【问题讨论】:

  • 首先你们从来没有使用过你的 UserViewModel。其次,我不确定您所说的“连接成功”是什么意思(我猜是登录成功),但如果是这种情况,您的用户身份验证不正确。
  • 是的,登录成功就是我的意思。我认为身份验证没问题,因为当我输入基础中存在的值时,它会将我带到带有 LogOn 链接的页面。但是,在相反的情况下,它会将我带到一个空白页面
  • 这个逻辑应该在你的控制器而不是你的页面中。您的帐户控制器中的代码是什么样的?
  • @KOL 请看一下编辑
  • 我没有看到您的操作链接在您添加的代码中调用的 LogOn 方法或 LogOff 方法

标签: c# sql-server asp.net-mvc-3 visual-studio-2010


【解决方案1】:

我没有看到您在哪里对用户进行身份验证?如果你试试这个:

if (!ValidateLogOn(Matricule, passWord))
{       
   return Connection(Matricule, passWord, returnUrl);
}

// user is valid, authenticate
FormsAuthentication.SetAuthCookie(Matricule, true);

【讨论】:

  • 感谢您的回答,但“密码”不被接受(在当前上下文中不存在),,,,我尝试使用“密码”,,,所有的声明都是红色的(重载方法)
  • 糟糕,抱歉,第二个参数是布尔值(持久性),请查看我的编辑
  • 哦,没问题,我改正了,我试了一下,但结果是一样的!对不起,但是你能看看我上面的对话吗,,,我认为问题在那里!
【解决方案2】:

您的 ActionLink 需要正确更新。

它应该采用上面示例中的格式:

<%: Html.ActionLink("Text on UI", "MethodNameInController", "ControllerName") %>

您在上面没有这样做 - 您的操作链接在控制器中没有方法。我还建议您阅读本教程 -

http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3

【讨论】:

  • 谢谢!我以前读过这个教程(这不是第一次有人建议我这个教程),,,我是初学者,,我知道我应该在 ActionLink 中放什么!第一个错误是因为我忘记改了!拜托,,,我认为问题不存在,,,因为登录用户的名称也没有出现!
猜你喜欢
  • 2012-06-22
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 2021-10-30
相关资源
最近更新 更多