When adding a controller for the model, the created actions will look something like this:
public ActionResult Edit(int id = 0)
{
Entity entity = db.Entities.Find(id);
if (entity == null)
{
return HttpNotFound();
}
return View(entity);
}
Now, in my case, I take a string identifier that can be mapped to database identifiers in several ways , creating several lines of code to retrieve the correct entity. Copying and pasting this code into every action that takes an identifier to receive an object feels very inelegant.
Entering the search code in a private controller function reduces the number of duplicate code, but I still remain with this:
var entity = GetEntityById(id);
if (entity == null)
return HttpNotFound();
? python, . - WCF, IOperationBehavior, - . - , , , , .
:
[EntityLookup(id => db.Entities.Find(id))]
public ActionResult Edit(Entity entity)
{
return View(entity);
}
EntityLookup string id Entity HttpNotFound, .