vishal patil

vishal patil

  • NA
  • 7
  • 6.1k

only UserID related data view then user login MVC4

Feb 12 2015 8:28 AM
How to UserId is automatically get value  in current user  in  UserId column in User table ??
&
how to view UserId related column data only view login user in Index in Asp .net Mvc 4 C#???

Models

    public class User
    {
        [Key]
        public long u_ID { get; set; }
        [Required(ErrorMessage = "Please Enter Headline name ")]
        [Display(Name = "Headline Profile")]
        public string head_profile { get; set; }
        [Required(ErrorMessage = "Please Enter Full Name")]
        [Display(Name = "Full Name:")]
        public string full_name { get; set; }
        [Display(Name = "Company Name:")]
        public string compa_name { get; set; }


        [Display(Name = "Home Location:")]
        public string job_loc { get; set; }


        public byte[] Image { get; set; }


        public long a_ID { get; set; }


        public virtual Area Areafk { get; set; }


        //[ForeignKey("UserProfile")]//use above or specify this
        public int? UserId { get; set; }
           


        public virtual UserProfile UserPro { get; set; }
    }

   [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public virtual ICollection <User> User { get; set; }
    }

Controller

     public ActionResult Index()
        {
            var users = db.Users.Include(u => u.Areafk).Include(u => u.UserPro);
         
            return View(users.ToList());
        }


     
  public ActionResult Create()
        {
            ViewBag.a_ID = new SelectList(db.Areas, "a_ID", "area");
            ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName");
            return View();
        }


        //
        // POST: /Job/Create


        [HttpPost]
       
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }


            ViewBag.a_ID = new SelectList(db.Areas, "a_ID", "area", user.a_ID);
            ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName", user.UserId);
            return View(user);
        }