title: How to get logged UserID in asp.net MVC and display it in hidden text box
body: <p>I can get username using @user.identity.name from table user but i need to get UserID instead of Username. how to do it?. i tried the following code in my controller but it returns 0.</p>
  
  <p><strong>My Controller:</strong></p>
  
  <pre><code>public ActionResult Index()
      {
          int userId = User.Identity.GetUserId&lt;int&gt;();
          User currentUser = db.Users.FirstOrDefault(x =&gt; x.UserID == userId);
          ViewBag.UID=userId;
          return View();
      }
  </code></pre>
  
  <p><strong>My View</strong></p>
  
  <pre><code> &lt;input value="@ViewBag.UID" type="hidden"/&gt;
  </code></pre>
  
  <p><strong>My Model</strong></p>
  
  <pre><code>[Table("User")]
  public partial class User
  {
      public int UserID { get; set; }
      //added by zakerr
      public int? DepartmentID { get; set; }
      public string Firstname{ get; set; }
      public string Lastname { get; set; }
  
  
      [Required]
      [StringLength(50)]
      public string Username { get; set; }
  
      [Required]
      [StringLength(50, MinimumLength =4)]
      [DataType(DataType.Password)]
      public string Password { get; set; }
  
  
      public string UserEmailAddress { get; set; }
  
      [StringLength(50)]
      public string Status { get; set; }
  
  
  }
  </code></pre>