none
Object reference not set to an instance of an object MVC3 & C# RRS feed

  • Question

  • public class AccountController : Controller
        {
    
            private LoginModel loginModel = null;
    
            #region Constructor
            public AccountController(LoginModel loginModel)
            {
                this.loginModel = loginModel;
            }
    
            public AccountController()
            {
            }
            #endregion
    
            #region Login
            //
            // GET: /Account/Login
            public ActionResult Login()
            {
                return View();
            }
    
            //
            // POST: /Account/Login
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Login(string userName, string password, bool rememberMe, string returnUrl)
            {
                try
                {
                    string displayFullName = null, token = null;
                    
                    LoginViewModel loginViewModel = new LoginViewModel();
                    loginViewModel.UserName = userName;
                    loginViewModel.Password = password;
                    loginViewModel.RememberMe = rememberMe;
    
                    ModelState.AddModelErrors(loginViewModel.ValidateLogIn());
    
                    if (!ModelState.IsValid)
                    {
                        return View();
                    }
    
                    //login failed than return view. 
                    if (!this.loginModel.LogIn(loginViewModel, ref displayFullName, ref token))
                    {
                        ModelState.AddModelError("_FORM", PortalErrors.IncorrectDataMsg);
                        return View();
                    }
    
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
    
                }
                catch (Exception exc)
                {
                    ModelState.AddModelError("_FORM", PortalErrors.CommonErrMsg);
                    return View();
                }
            }

    Hi, I get the error on my above code on line if (!this.loginModel.LogIn(loginViewModel, ref displayFullName, ref token))

    Can any one help with this to find what's wrong in this one?

    Thank.

    Tuesday, February 21, 2012 3:38 PM

All replies

  • Hi ujmvc,

    Welcome to MSDN Forum.

    Could you please post the code of the Login method and what's the error information?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us


    Thursday, February 23, 2012 1:38 AM
    Moderator
  • Hi ujmvc,

    Have you solved the issue? I'm waiting forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 27, 2012 1:46 AM
    Moderator