что мне не хватает?
Папка для пользовательских моделей:
public class StatusModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// Fetch the id from the RouteData
var id = controllerContext.RouteData.Values["id"] as string;
// TODO: Use constructor injection to pass the service here
var status = sService.GetStatusById(id);
// Compare whether the id passed in the request belongs to
// the currently logged in user
if (status.UserId != GetUserId())
{
throw new HttpException(403, "Forbidden");
}
return status;
}
private Guid GetUserId()
{
if (Membership.GetUser() != null)
{
MembershipUser member = Membership.GetUser();
Guid id = new Guid(member.ProviderUserKey.ToString());
return id;
}
return Guid.Empty;
}
}
и тогда вы зарегистрируете эту модель связующего в Application_Start
:
// Could use constructor injection to pass the repository to the model binder
ModelBinders.Binders.Add(typeof(Status), new StatusModelBinder());
и наконец
// The authorize attribute ensures that a user is authenticated.
// If you want it to redirect to /Home/Index as in your original
// example if the user is not authenticated you could write a custom
// Authorize attribute and do the job there
[Authorize]
public ActionResult EditStatus(Status status)
{
// if we got that far it means that the user has access to this resource
// TODO: do something with the status and return some view
...
}
Вывод: мы поставили этот контроллер на диету, которая должна быть такой, какой должны быть контроллеры: -)