title: MVC5: Allow user to only view and edit their own information
body: <p>I created a scaffold based on one of my tables.  I'm now trying to figure out how to allow the user to only view/edit data that is the same as their userID.</p>
  
  <p>Right now it displays all the data from that table and allows for editing of all users.  I need to be able to limit it to only the user that is currently logged in. </p>
  
  <p>Please let me know if I can give any more details.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>    public class ClientViewStaffController : Controller
  {
      private TpsEntities db = new TpsEntities();
  
      // GET: ClientViewStaff
      public ActionResult Index()
      {
          return View(db.staffTables.ToList());
      }
  
      // GET: ClientViewStaff/Details/5
      public ActionResult Details(int? id)
      {
          if (id == null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          staffTable staffTable = db.staffTables.Find(id);
          if (staffTable == null)
          {
              return HttpNotFound();
          }
          return View(staffTable);
      }
  
      // GET: ClientViewStaff/Create
      public ActionResult Create()
      {
          return View();
      }
  
      // POST: ClientViewStaff/Create
      // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
      // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include = "staffID,staffFirstName,staffLastName,staffTitle,staffAddress,staffCity,staffState,staffZip,staffExperience,staffEducation,desiredSalary,staffProfession,staffAvailibity,staffPhoto,staffEmail,staffPhoneNum,userID")] staffTable staffTable)
      {
          if (ModelState.IsValid)
          {
              db.staffTables.Add(staffTable);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          return View(staffTable);
      }
  
      // GET: ClientViewStaff/Edit/5
      public ActionResult Edit(int? id)
      {
          if (id == null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          staffTable staffTable = db.staffTables.Find(id);
          if (staffTable == null)
          {
              return HttpNotFound();
          }
          return View(staffTable);
      }
  
      // POST: ClientViewStaff/Edit/5
      // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
      // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Edit([Bind(Include = "staffID,staffFirstName,staffLastName,staffTitle,staffAddress,staffCity,staffState,staffZip,staffExperience,staffEducation,desiredSalary,staffProfession,staffAvailibity,staffPhoto,staffEmail,staffPhoneNum,userID")] staffTable staffTable)
      {
          if (ModelState.IsValid)
          {
              db.Entry(staffTable).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(staffTable);
      }
  
      // GET: ClientViewStaff/Delete/5
      public ActionResult Delete(int? id)
      {
          if (id == null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          staffTable staffTable = db.staffTables.Find(id);
          if (staffTable == null)
          {
              return HttpNotFound();
          }
          return View(staffTable);
      }
  
      // POST: ClientViewStaff/Delete/5
      [HttpPost, ActionName("Delete")]
      [ValidateAntiForgeryToken]
      public ActionResult DeleteConfirmed(int id)
      {
          staffTable staffTable = db.staffTables.Find(id);
          db.staffTables.Remove(staffTable);
          db.SaveChanges();
          return RedirectToAction("Index");
      }
  
      protected override void Dispose(bool disposing)
      {
          if (disposing)
          {
              db.Dispose();
          }
          base.Dispose(disposing);
      }
  }
  }
  </code></pre>
  
  <p><strong>View (Index.cshtml)</strong></p>
  
  <pre><code>&lt;table class="table"&gt;
  &lt;tr&gt;
      &lt;th&gt;
          First Name
      &lt;/th&gt;
      &lt;th&gt;
          Last Name
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.staffState)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.staffExperience)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.staffEducation)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.desiredSalary)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.staffProfession)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.staffAvailibity)
      &lt;/th&gt;
      &lt;th&gt;&lt;/th&gt;
  &lt;/tr&gt;
  
  @foreach (var item in Model)
  {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.staffFirstName)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.staffLastName)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.staffState)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.staffExperience)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.staffEducation)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.desiredSalary)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.staffProfession)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.staffAvailibity)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.ActionLink("Details", "Details", new { id = item.staffID }) |
  
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  </code></pre>
  
  <p></p>
  
  <p><strong>View (Details.cshtml)</strong></p>
  
  <pre><code>&lt;div&gt;
  &lt;h4&gt;Staff View&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;dl class="dl-horizontal"&gt;
      &lt;dt&gt;
          First Name
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffFirstName)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Last Name
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffLastName)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Title
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffTitle)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Address
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffAddress)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          City
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffCity)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          State
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffState)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Zip Code
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffZip)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Experience
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffExperience)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Education
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffEducation)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Salary
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.desiredSalary)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Profession
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffProfession)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Availability
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffAvailibity)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Photo
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffPhoto)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Email
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffEmail)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          Phone Number
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.staffPhoneNum)
      &lt;/dd&gt;
  
      &lt;dt&gt;
          User ID
      &lt;/dt&gt;
  
      &lt;dd&gt;
          @Html.DisplayFor(model =&gt; model.userID)
      &lt;/dd&gt;
  
  &lt;/dl&gt;
  &lt;/div&gt;
  &lt;p&gt;
      @Html.ActionLink("Edit", "Edit", new { id = Model.staffID }) |
     @Html.ActionLink("Back to List", "Index")
  &lt;/p&gt;
  </code></pre>