我创建了一个基于我的表之一的脚手架。我现在试图找出如何让用户只能查看/编辑与他们的用户ID相同的数据。MVC5:允许用户只查看和编辑自己的信息
现在它显示来自该表的所有数据并允许编辑所有用户。我需要能够仅限于当前登录的用户。
请让我知道,如果我可以提供任何更多的细节。
控制器
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);
}
}
}
视图(Index.cshtml)
<table class="table">
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
@Html.DisplayNameFor(model => model.staffState)
</th>
<th>
@Html.DisplayNameFor(model => model.staffExperience)
</th>
<th>
@Html.DisplayNameFor(model => model.staffEducation)
</th>
<th>
@Html.DisplayNameFor(model => model.desiredSalary)
</th>
<th>
@Html.DisplayNameFor(model => model.staffProfession)
</th>
<th>
@Html.DisplayNameFor(model => model.staffAvailibity)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.staffFirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.staffLastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.staffState)
</td>
<td>
@Html.DisplayFor(modelItem => item.staffExperience)
</td>
<td>
@Html.DisplayFor(modelItem => item.staffEducation)
</td>
<td>
@Html.DisplayFor(modelItem => item.desiredSalary)
</td>
<td>
@Html.DisplayFor(modelItem => item.staffProfession)
</td>
<td>
@Html.DisplayFor(modelItem => item.staffAvailibity)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.staffID }) |
</td>
</tr>
}
视图(Details.cshtml)
<div>
<h4>Staff View</h4>
<hr />
<dl class="dl-horizontal">
<dt>
First Name
</dt>
<dd>
@Html.DisplayFor(model => model.staffFirstName)
</dd>
<dt>
Last Name
</dt>
<dd>
@Html.DisplayFor(model => model.staffLastName)
</dd>
<dt>
Title
</dt>
<dd>
@Html.DisplayFor(model => model.staffTitle)
</dd>
<dt>
Address
</dt>
<dd>
@Html.DisplayFor(model => model.staffAddress)
</dd>
<dt>
City
</dt>
<dd>
@Html.DisplayFor(model => model.staffCity)
</dd>
<dt>
State
</dt>
<dd>
@Html.DisplayFor(model => model.staffState)
</dd>
<dt>
Zip Code
</dt>
<dd>
@Html.DisplayFor(model => model.staffZip)
</dd>
<dt>
Experience
</dt>
<dd>
@Html.DisplayFor(model => model.staffExperience)
</dd>
<dt>
Education
</dt>
<dd>
@Html.DisplayFor(model => model.staffEducation)
</dd>
<dt>
Salary
</dt>
<dd>
@Html.DisplayFor(model => model.desiredSalary)
</dd>
<dt>
Profession
</dt>
<dd>
@Html.DisplayFor(model => model.staffProfession)
</dd>
<dt>
Availability
</dt>
<dd>
@Html.DisplayFor(model => model.staffAvailibity)
</dd>
<dt>
Photo
</dt>
<dd>
@Html.DisplayFor(model => model.staffPhoto)
</dd>
<dt>
Email
</dt>
<dd>
@Html.DisplayFor(model => model.staffEmail)
</dd>
<dt>
Phone Number
</dt>
<dd>
@Html.DisplayFor(model => model.staffPhoneNum)
</dd>
<dt>
User ID
</dt>
<dd>
@Html.DisplayFor(model => model.userID)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.staffID }) |
@Html.ActionLink("Back to List", "Index")
</p>
Couldyou提供您的域模型的一些细节? – 2014-10-19 18:18:10
我从来没有解释过,所以请裸露在我身边。我设置视图,查看模型,控制器。我使用MS的MVC 5模板创建项目,并放弃了我不需要的东西。我使用个人身份验证,因为我已连接到我的SQL服务器(不是本地)。 Model.edmx在那里...让我知道你特别要求什么,稍微有点新鲜。 – slider1578 2014-10-19 18:24:57
您是否使用asp.net身份框架进行身份验证?如果你很乐意为你的编辑按钮使用显示/隐藏选项,那么我可以给你简单的解决方案。 – DSR 2014-10-19 18:29:56