views:

39

answers:

3

How would one go about performing controller actions from withing an ASP.net MVC user control?

My scenario is that I have a userID and I want to transform it into a name from the database. To do this I've annotated my model with a display type and put a User template in the shared display templates but I'm not sure where I should write the code which does the lookup to convert from userID to user name.

+1  A: 

I think that code ought to go into your models and you should be calling it in your controller and passing it to your user-control in a viewdata. This is if I understood your question.

Cyril Gupta
+1  A: 

I would just have the model expose the name and not the userID. This way your view (user control) is only displaying the name and not trying to do a DB lookup. Your "User Control" model would be responsible for how it gets the name, i.e. the DB from your question.

confusedGeek
+1  A: 

In short, you don't do that.

You should be passing the necessary data to the MVC user control from the View, which in turn should be getting it's information from the controller.

The view (or user control) should not have any knowledge of the controller. You may want to use RenderAction instead of a user control if you feel that the view shouldn't be responsible for passing the necessary information into the user control.

mkedobbs