Racing57
Racing57

Reputation: 91

mvc4 model is null when RedirectToAction calls view

I will start by saying I am new to MVC4... so be gentle

I have a model

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

which is just an extention of the standard log in model, I have just added the CurrentBalance variable.

I have then added code to the AccountModel which uses the username and password to log into another system, on a successful log in I update the CurrentBalacnce value with the returned value, and then use RedirectToAction to load the logged in page.

[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);
}

as you can see I am using the standard code for now while I get my head round it all, but when I then load the index page I get a null value in the 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>

I am sure I am doing something very basic wrong... but any help would be much appreciated, as going forward I will need to pass variables to and from the views..

Thanks in advance

Upvotes: 1

Views: 2105

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039588

You cannot pass complex objects when redirecting. You will have to explicitly decide which properties of this model you want to be send along with the redirect as query string parameters:

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
    }
);

Actually the correct way to do this is to not send any parameter when redirecting:

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

and then inside the target action you will be able to retrieve the currently authenticated user from the forms authentication 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
    ...
}

Upvotes: 4

Related Questions