2013-05-10 133 views
0

我正在开发一个ASP.Net MVC 3应用程序使用C#和SQL Server 2005.我使用实体框架与代码优先的方法。部分视图空页面ASP 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; 
     } 
+0

那么首先你甚至从来没有使用UserViewModel。其次,我不确定“连接成功”的意思(我认为,登录成功),但如果是这种情况,则表示您没有正确认证用户。 – 2013-05-10 08:53:20

+0

是的,登录successeded这是我的意思。我认为认证是正确的,因为当我把基数中存在的值放到一个带有LogOn链接的页面上时。然而,在相反的情况下,它带我到一个空白页面 – anouar 2013-05-10 09:06:27

+0

这个逻辑应该在你的控制器而不是你的页面。代码在您的账户控制器中看起来像什么? – 2013-05-10 09:30:36

回答

1

我没有看到你对用户进行认证?如果你尝试这样做:

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

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

感谢您的答案,但'密码'不accerounded(不存在于当前上下文中),,,,我尝试与'密码',,,所有语句在红色(重载方法) – anouar 2013-05-10 09:55:31

+0

哎呀抱歉,第二个参数是一个布尔(持久性),检查我的编辑 – 2013-05-10 10:01:33

+0

哦,没问题,我正确,我试试它,但结果是一样的! 对不起,但你可以看看我的对话上面,,,我认为探头就在那里! – anouar 2013-05-10 10:04:42

1

你的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

+0

谢谢!我之前读过这个tuto(这不是第一次有人给我推荐这个tuto),,,我是初学者,我知道我应该在ActionLink中放置什么!第一个错误,因为我忘记改变它!请,,,我不认为问题在那里,,,因为也登录用户的名称不会出现! – anouar 2013-05-10 10:36:36