当RedirectToAction调用视图时,mvc4模型为null

时间:2013-01-04 16:07:47

标签: c# view model asp.net-mvc-4 controller

我首先要说我是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加载登录页面。 / p>

[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>

我确信我正在做一些非常基本的错误...但是任何帮助都会非常感激,因为未来我将需要将变量传递给视图和从视图传递..

提前致谢

1 个答案:

答案 0 :(得分: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
    ...
}
相关问题