First, a little bit of context (cause I know you all love it!):
I have a model called Note. It has a field called Type which, in the database, is an integer value that designates which type of object this note belongs to (1 = Customer, 2 = Sales Lead, etc). A second field called Source designates the ID of the specific object that this note belongs to.
For example, if I wanted to add a note to Customer #34, I would set Type to 1 and Source to 34.
To help make things a little more readable, I have an Enum called DataObjectType that contains a mapping for each of the possible Type integer values to a friendly name. So, for example, DataObjectType.Customer is the same as a value of 1 in the Type field.
Next, I have a custom route:
/Notes/New/{type}/{id}
and two controller actions to handle the creation of a new note:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult New(DataObjectType type, int id) {
Note note = new Note { Type = type, Source = id };
return View(note);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Note note) {
// This is just an example
note.Persist();
return View("New", note);
}
First of all, I need to make sure that the value supplied to the {type} route value is valid for the DataObjectType enum. Can this be done with a route constraint? At the moment, the type argument of New() will accept any integer, whether it exists in DataObjectType or not.
Second, I need to make sure that the object the user is trying to add the note to actually exists in the database before showing the New Note form at all. I could do this in the New() method easily, but there may come a time when a user posts the data directly to Create() without ever visiting New(). In such a case, I also need to verify that the object exists in the Create() method. What's the best way to do this in both methods without duplicating code?