views:

43

answers:

3

I am following the tutorial of scott gu from Here to retrieve the data using stored procedure. i am setting the return type of the stored procedure by dropping the SProc on class. Now my question is that can we set some viewmodel as the return type of the stored procedure, as my view is strongly typed with my viewmodel

A: 

You can associate the return type of the stored procedure to some custom type: In .edmx design click on the imported stored procedure and set the function import name to SomeCustomName and select the return Type -> Entities -> SomeEntity. Now having the stored procedure return entity models you could map those models to a view model that is eventually returned to the view. It is considered good practice to have view models tailored to the views instead of passing directly the SQL transport objects.

Darin Dimitrov
I am using linq to sql
Tassadaque
A: 

[I have yet to do anything with ASP.NET MVC, so this suggestion may be way off, but...]

One option would be to create your ViewModel class in your dbml designer by going right-click->Add->Class. Then, either drop your stored procedure on the new class in the designer, or if that fails (like it often does, for me), drop the stored procedure onto the designer surface and set its return type (Properties -> Return Type) to the new class.

That's one way to have a stored procedure return a custom type that's not a true LINQ to SQL entity and not an autogenerated stored procedure *Result class. (Since I've heard it's bad form to pass model objects to the view - ideally you pass a ViewModel object, right?).

shaunmartin
whenever dbml will be recreated this class will be washed away..
Tassadaque
I'm not suggesting creating the class in your .Designer.cs file - surely anything manually created there *will* be removed on the next dbml save. What I'm suggesting is creating the class in the entity/dbml designer just like your regular LINQ to SQL entity classes. Except instead of creating the table by dragging the database table onto the surface, you just create the class right there using the right-click->Add->Class procedure I mentioned. Anything created in the designer using this method will be maintained when the the .Designer.cs file is regenerated.
shaunmartin
A: 

I have written a database view instead of viewmodel and then drag the view to dbml and set the return type of stored procedure to view. That solve my problem. It may be helpful for some one else

Tassadaque