6
Answers

UserId from the table is null when controller is called?

Photo of Guest User

Guest User

5y
786
1
Hi team
 
I have procedure for Usp_UserNamebyUserId and has UserId, but on the controller for login, its state as null. How can i fix this issue? Here is my logic from the controller.
 
// login.cshtml
  1. <div class="form-group">  
  2. <input id="BtnLogin" type="submit" class="btn btn-success btn-pressure" name="BtnLogin" value="Login" />  
  3. <input id ="btnSubmit" type ="submit" class="btn btn-info btn-pressure" name="btnSubmit" value="Create" />  
  4. </div>  
  5. </div>  
// controller.cs
  1. [HttpPost]  
  2. [AllowAnonymous]  
  3. [ValidateAntiForgeryToken]  
  4. public ActionResult CreateLogin(LoginCreateModel objSubmit)  
  5. {  
  6. if(objSubmit.btnSubmit == "Create")  
  7. {  
  8. ViewBag.Message = "Details saved successfully";  
  9. }  
  10. return View(objSubmit);  
  11. }  
  12. [HttpPost]  
  13. [AllowAnonymous]  
  14. [ValidateAntiForgeryToken]  
  15. public ActionResult Login(Login login)  
  16. {  
  17. if (ModelState.IsValid)  
  18. {  
  19. bool success = WebSecurity.Login(login.username, login.password, false);  
  20. var UserID = GetUserID_By_UserName(login.username);  
  21. var LoginType = GetRoleBy_UserID(Convert.ToString(UserID)); // its null when its debugged  
  22. if (success == true)  
  23. {  
  24. if (string.IsNullOrEmpty(Convert.ToString(LoginType)))  
  25. {  
  26. ModelState.AddModelError("Error""Rights to User are not Provide Contact to Admin");  
  27. return View(login);  
  28. }  
  29. else  
  30. {  
  31. Session["Name"] = login.username;  
  32. Session["UserID"] = UserID;  
  33. Session["LoginType"] = LoginType;  
  34. if (Roles.IsUserInRole(login.username, "Admin"))  
  35. {  
  36. return RedirectToAction("AdminDashboard""Dashboard");  
  37. }  
  38. else  
  39. {  
  40. return RedirectToAction("UserDashboard""Dashboard");  
  41. }  
  42. }  
  43. }  
  44. else  
  45. {  
  46. ModelState.AddModelError("Error""Please enter valid Username and Password");  
  47. return View(login);  
  48. }  
  49. }  
  50. else  
  51. {  
  52. ModelState.AddModelError("Error""Please enter Username and Password");  
  53. return View(login);  
  54. }  
  55. }  

Answers (6)

0
Photo of Guest User
Tech Writer 2.1k 524.5k 5y
Jignesh no its not
 
CREATE proc [dbo].[Usp_getRoleByUserID]
@UserId int
as
begin
select R.RoleName FROM webpages_UsersInRoles WR
inner JOIN webpages_Roles R on WR.RoleId = R.RoleId
where WR.UserId =@UserId
end
GO
 
0
Photo of Jignesh Kumar
29 39.5k 2.9m 5y
Hey,
Please check you store procedure is retruning data or not ?  if not then Share your store procedure here?
0
Photo of Guest User
Tech Writer 2.1k 524.5k 5y
Should this before @using(Html.BeginForm() {.......}?
0
Photo of Rupesh Kahane
100 19.1k 4.2m 5y
Please try to keep the UserId in hidden field or in any hidden text, so while submit button you can pass that UserId to controller, so you can easily get UserId in the controller.
 
In view store the UserId in hidden fields 
  1. @Html.HiddenFor(x => x.UserId)  
 
 
Let me know below 
1) Are you calling action from jquery to submit data? or are you calling @BeginForm in the .cshtml page  directly. 
2) How can you get User id in controller?? 
0
Photo of Guest User
Tech Writer 2.1k 524.5k 5y
[NonAction]
public string GetRoleBy_UserID(string UserId)
{
return objIAccountData.GetRoleByUserID(UserId);
}
0
Photo of Ramachandran M
NA 713 23.3k 5y
Hi,
Can you post GetUserID_By_UserName , GetRoleBy_UserID method details ?