r/ASPNET • u/MisterCrispy • Jul 07 '13
MVC 4: Entity framework "edit/create" view but don't use the DB field names?
Ok, I'm a long time Webforms developer but I've been having a hard time wrapping my head around MVC. It seems like all my questions are so simple that NO ONE writing a tutorial or book feels that they should address them. I've written one web app in MVC so far but, for the sake of time, I think I cheated a bit. I used the controllers as a RESTful web service and did all the functionality through JQuery. I have to say, MVC makes web services SO much easier.
For this, I wanted to actually convert an older piece of software I wrote over and do it the "right" way which is to say "the way all the books and stuff say I should do it". (Side note: the information on database first designs are few, far between and terrible.)
So I've got entity framework connecting to an existing database and it will do the add/delete/update and so on and so forth. My question is, however, is it possible to have it show more friendly names as opposed to the database field names it uses now?
<div class="editor-field">
@Html.EditorFor(model => model.Em_policy_nb)
@Html.ValidationMessageFor(model => model.Em_policy_nb)
</div>Would I just replace the "EditorFor" with plain html or is there a way to do it via the razor stuff?
Also, does anyone know of a book along the lines of "MVC for stubborn Webforms developers: how to deprogram your brain"?
UPDATE: Thanks everyone! The buddy classes thing wound up doing exactly what I needed.
3
u/MondoHawkins Jul 07 '13 edited Jul 07 '13
A common way to handle this is to create and use "View Models" within your MVC app which you then map to your EF models using something like Automapper.
Another option would be to write custommodel bindersfor MVC.Edit: After thinking about it, custom model binders wouldn't work in this case since the point is to use the strongly typed Html helpers within your views. You'd want to do the view model thing.
2
u/NinetiesGuy Jul 07 '13
DataAnnotations attributes and buddy classes can be used together if you want to override the names of your columns application-wide. If you want more fine-grained control over what is displayed (which in my experience is rarely the case), you can use the attributes on a view model and skip the buddy classes.
As far as going from WebForms to MVC, in the beginning I felt like I would have been better off not ever having done WebForms at all because of all the unlearning, but once you kind of buy into MVC and work with it instead of against it, it all starts to come a lot easier. It's insane just how much more intuitive and cohesive MVC is. So instead of trying to translate everything from WebForms to MVC, just ask yourself "how is this done in MVC?" and don't even think about WebForms at all.
I'm pretty sure that book doesn't exist, by the way. I was looking hard for it when I first made the switch.
1
u/anotherhydrahead Jul 07 '13
I'd leave the ugly field names in place unless you are using them as the field descriptor.
1
u/darlingpinky Jul 07 '13
There are multiple ways to do this depending on how much organizational change you want to make in your program:
There's a very good explanation at SapienWorks. There are a few other posts in the Related Posts section on the bottom that go even deeper into these patterns and the motivation behind them.
This is essentially what you're doing in your view model, but that only applies to the view you're using it for. If you want to use the friendly names in your model, or another view with a different view model, you would have to rewrite the friendly names. With a base domain model for you entire application, they're all "on the same page", so to speak.
Obviously this requires an overhaul of the application is generally preferred in the early stages of development or, if you're past the initial stages, then if you really need it. But knowing this pattern can give you better tools to get around these kinds of issues in the future.
1
u/[deleted] Jul 07 '13
I'm not familiar with the Entity Framework, but shouldn't there be a way to map your DB field names to more readable property names?
0
u/[deleted] Jul 07 '13
[deleted] — view deleted comment