我創建了一個基於我的表之一的腳手架。我現在試圖找出如何讓用戶只能查看/編輯與他們的用戶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