Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 466.6k

UserId from the table is null when controller is called?

Jan 27 2020 12:49 PM
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)