如果 move.UserId 不等于 currentUserId 则尝试构建异常然后重定向到操作,否则如果 move.UserId 不等于 currentUserId 返回视图。
请参阅此处的代码:
public ActionResult Details(int id)
{
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
Move move = db.Moves.Where(m => m.UserId == currentUserId)
.FirstOrDefault();
if (currentUser != null && currentUser.ProviderUserKey != null && currentUser.IsApproved)
{
if (move.UserId == currentUserId)
{
return View(move);
}
}
return RedirectToAction("Oops", new RouteValueDictionary(
new { controller = "Account", action = "Oops", area = "", id = UrlParameter.Optional }));
}
我想将它绑定到将带回 Move/(int) 的 url,这样如果用户将其修改为返回 move.UserId != currentUserId 的移动的 (int),那么它们也会重定向。目前可以修改Url来获取其他人的招式。
我的控制器
public ViewResult Index()
{
if (User.Identity.IsAuthenticated)
{
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
if (currentUser != null && currentUser.ProviderUserKey != null && currentUser.IsApproved)
{
var results = db.Moves.Where(move => move.UserId == currentUserId)
.ToList();
return View(results);
}
}
return View(db.Moves.ToList());
}
[ClientValidation]
public ActionResult Details(Move move)
{
return View(move);
}
我的观点
@model MovinMyStuff.Domain.Entities.Move
@{
ViewBag.Title = "Details";
}
<div>
@Html.DisplayFor(model => model.StartCity),
@Html.DisplayFor(model => model.StartState)
@Html.DisplayFor(model => model.StartZip) -
@Html.DisplayFor(model => model.EndCity),
@Html.DisplayFor(model => model.EndState)
@Html.DisplayFor(model => model.EndZip)
</div>
<fieldset>
<div class="job-details">
@Html.HiddenFor(model => model.MoveId)
@Html.HiddenFor(model => model.UserId)
<ul class="distance">
<li>
<div>
Distance</div>
</li>
<li>1,978.6 Miles</li>
</ul>
<ul class="address-wrapper">
<li>
<ul class="address from">
<li>
<div>
From</div>
</li>
<li><span>Address: </span>
@Html.DisplayFor(model => model.StartStreetNumber)
@Html.DisplayFor(model => model.StartStreetName)
</li>
...
</fieldset>