skip to Main Content

Receiving Null value every time to Controller. How can I solve this?

Url: http://localhost:45801/Account/Login?ReturnUrl=%2Forder%2Fplaceorder

Code:

    [HttpPost]
    public async Task<IActionResult>Login(UserLoginVM userLoginVM,string ReturnUrl)
    {
        if (ModelState.IsValid)
        {
            var result = await signInManager.PasswordSignInAsync
                (userLoginVM.LoginId, userLoginVM.Password, userLoginVM.RememberMe, false);

            if(result.Succeeded)
            {
                if(!string.IsNullOrEmpty(ReturnUrl))
                {
                    return RedirectToAction(ReturnUrl);
                }
                else
                {
                   return RedirectToAction("Index", "Home");
                }    
            }
            ModelState.AddModelError("","Invalid username and password");
        }
        return View(userLoginVM);
    }

2

Answers


  1. You need to add [FromBody] before UserLoginVM and [FromQuery] before string , so your action method sign should be like this :

    public async Task<IActionResult>Login([FromBody] UserLoginVM userLoginVM,[FromQuery] string ReturnUrl)
    
    Login or Signup to reply.
  2. You should add [FromQuery] or [FromBody] attribute to the parameters.

    Adding [FromBody] attribute to the parameter, force Web API to read from the request body.

    Adding [FromQuery] attribute to the parameter, force Web API to gets values from the query string.

    [HttpPost]
    public async Task<IActionResult>Login([FromBody] UserLoginVM userLoginVM,[FromQuery] string ReturnUrl)
    {
    ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search