2013-05-10 132 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