2013-01-04 42 views
1

我將開始......所以,請溫柔mvc4機型爲空時RedirectToAction說我是新來MVC4調用視圖

我有一個模型

public class LoginModel 
{ 
    [Required] 
    [Display(Name = "User name")] 
    public string UserName { get; set; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [Display(Name = "Remember me?")] 
    public bool RememberMe { get; set; } 

    public double CurrentBalance { get; set; } 
} 

這僅僅是標準的一種推廣登錄模型,我剛剛添加了CurrentBalance變量。

然後我添加了代碼到AccountModel,它使用用戶名和密碼登錄到另一個系統,成功登錄後,我使用返回的值更新CurrentBalacnce值,然後使用RedirectToAction加載登錄頁面。

[AllowAnonymous] 
[HttpPost] 
public ActionResult Login(LoginModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     //Log into the server 

     if (server_loggedIn) 
     { 
      server.LogOut(); 
     } 
     if (server.LogIn("****", "****", "****") == 0) 
     { 
      if (server.GetUserInfoByUserName(model.UserName) == 0) 
      { 
       if (server.GetUserTransactionInfo(model.UserName) == 0) 
       { 

        model.UserName = server.m_sLoggedInUser; 
        model.CurrentBalance = server.m_currentBalance; 
        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 

        return RedirectToAction("Index","Account", new {model}); 
       } 
      } 
      } 
     else 
     { 
      ModelState.AddModelError("", "The user name or password provided is incorrect."); 
     } 
    } 

    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

,你可以看到我使用的標準代碼趁現在我讓我的頭一輪的這一切,但是當我再加載索引頁我在模型中得到一個空值

@model Print_Management.Models.LoginModel 

@{ 
    ViewBag.Title = "Your Account"; 
} 

@section Header { 
    @Html.ActionLink("Back", "Index", "Home", null, new { data_icon = "arrow-l", data_rel = "back" }) 
    <h1>@ViewBag.Title</h1> 
} 

<p> 
    Logged in as <strong>@User.Identity.Name</strong>. 
    /n\nCurrent Balance <strong>@Model.CurrentBalance</strong> 
</p> 

<ul data-role="listview" data-inset="true"> 
    <li>@Html.ActionLink("Deposit", "ChangePassword")</li> 
    <li>@Html.ActionLink("Log off", "LogOff")</li> 
</ul> 

我相信我做的事情很基本的錯誤......但任何幫助,將不勝感激,因爲前進我需要傳遞變量和意見..

在此先感謝

回答

4

重定向時無法傳遞複雜對象。你必須明確地決定要與重定向作爲查詢字符串參數一起發送此模型的屬性:

return RedirectToAction(
    "Index", 
    "Account", 
    new { 
     username = model.UserName, 
     password = model.Password, // oops, be careful the password will appear in the query string 
     rememberMe = model.RememberMe, 
     currentBalance = model.CurrentBalance 
    } 
); 

其實重定向時要做到這一點是不發送任何參數的正確方法是:

return RedirectToAction("Index", "Account"); 

,然後你就可以檢索窗體身份驗證Cookie在當前已驗證用戶的目標行爲裏面:

[Authorize] 
public ActionResult Index() 
{ 
    string username = User.Identity.Name; 

    // Now that you know who the current user is you could easily 
    // query your data provider to retrieve additional information about him 
    ... 
} 
+0

達林您好,我會嘗試都方法,但是,如何在視圖中使用變量?我已經改變了我的控制器現在使用單個元素,而不是一個複雜的變量,對不起,但正如我所說,我是MVC的新手,並且使用model.CurrentValue不起作用,因爲模型變量仍然爲空 – Racing57

+0

任何人 ??如果可能的話,我想在星期一回到工作之前,先進行排序......謝謝 – Racing57

+0

哦,剛剛意識到,當我從視圖頁面使用用戶變量時,我無法訪問我添加的currentbalance變量? – Racing57