Вместо того, чтобы передавать идентификаторы в действия вашего контроллера, напишите пользовательский связыватель модели для ваших сущностей, который будет извлекать его из базы данных.Например, предположим, что у вас есть следующая модель:
public class Person
{
public string Id { get; set; }
... some other properties
}
Теперь вместо:
[HttpPost]
public ActionResult Edit(string id)
{
...
}
напишите:
[HttpPost]
public ActionResult Edit(Person person)
{
...
}
и затем напишитепользовательская модель привязки для персоны:
public class PersonModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var id = bindingContext.ValueProvider.GetValue("id");
// check if an id was provided and if the user is authenticated
if (!controllerContext.HttpContext.User.Identity.IsAuthenticated || id == null)
{
throw new HttpException(403, "Forbidden");
}
var currentUser = controllerContext.HttpContext.User.Identity.Name;
// fetch the person from your repository given the id and belonging
// to the currently authenticated user
var person = _repository.GetPerson(id.AttemptedValue, currentUser);
if (person == null)
{
// no person found matching
throw new HttpException(403, "Forbidden");
}
return person;
}
}
, которую вы зарегистрируете в Application_Start
:
ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder());