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

标签 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,在成功登录后,我用返回值更新 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
    ...
}

关于c# - 当 RedirectToAction 调用 View 时,mvc4 模型为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14160602/

相关文章:

c# - 将表单标记添加到 ASP.Net Webforms 页面时出现空堆栈错误

c# - 为什么这个自定义字典类不起作用 - C# 4.0

spring - 当 Spring 无法验证模型的 java.util.Date 字段时,如何显示用户友好的错误?

error-handling - 覆盖获得 fatal error : Call to undefined method的magento模型文件

JavaFX(带有 FXML)MVC : Model use Controller

c# - 数据库是我的 C# 应用程序数据存储的正确选择吗?

c# - 通过 TCP 读取嗅探数据

Python 获取 SettingWithCopyWarning - iloc 与 loc - 无法弄清楚原因

android - 为什么在尝试为按钮调用方法时程序崩溃?

python - Django 多选字段验证