Skip to main content
 首页 » 编程设计

c#之当 RedirectToAction 调用 View 时,mvc4 模型为 null

2024年02月20日13傻小

首先我会说我是 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,在成功登录后,我用返回值更新 CurrentBalancce 值,然后使用 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> 

我确定我在做一些非常基本的错误......但是任何帮助将不胜感激,因为今后我将需要在 View 之间传递变量......

提前致谢

请您参考如下方法:

重定向时不能传递复杂的对象。您必须明确决定要将此模型的哪些属性与重定向一起作为查询字符串参数发送:

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 
    ... 
}