【问题标题】:mvc4 model is null when RedirectToAction calls viewRedirectToAction 调用视图时 mvc4 模型为空
【发布时间】:2013-01-04 16:07:47
【问题描述】:

首先我会说我是 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>

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

提前致谢

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 嗨达林,我会尝试这两种方法,但是,如何在视图中使用变量?我现在已经将控制器更改为使用单个元素,而不是复杂变量,抱歉,但就像我说的,我是 MVC 的新手,并且使用 model.CurrentValue 仍然不起作用,因为模型变量仍然是 null
    • 有人吗?如果可能的话,我想在星期一回来工作之前解决这个问题......谢谢
    • 哦,刚刚意识到,当我从视图页面使用用户变量时,我无法访问我添加的 currentbalance 变量?
    • 但是你为什么要在LoginModel 上添加这个变量呢?这没有意义。 LoginModel 应仅在身份验证时使用。在您的用户配置文件中添加此变量会更有意义。然后,当您在 Index 操作中查询它时,一旦通过身份验证,您就可以从数据库中检索该值。
    • 这会更有意义,但我把它放在那里是因为我使用 loginmodel 登录到我的其他服务器,正如我在一开始所说的那样,我是 MVC 的新手,但我是拿起它,那么我将如何将它添加到用户配置文件中,因为我同意这会更好..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多