Receiving Null value every time to Controller. How can I solve this?
Url: http://localhost:45801/Account/Login?ReturnUrl=/order/placeorder
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);
}
CodePudding user response:
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)
CodePudding user response:
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)
{
...
}
CodePudding user response:
You must send the return url in the get method to view using ViewData as shown below.
[HttpGet]
public IActionResult Login(string ReturnUrl)
{
ViewData["ReturnUrl"]=ReturnUrl;
return View();
}
and your view should like this :
<form asp-controller="Account" asp-action="Login"
asp-route-returnurl="@ViewData["ReturnUrl"]"
method="post" >
</form>