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
views:
43answers:
3You 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.
[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?).
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