4

I have a MVC3 web application where I need to implement a database search functionality. I am creating a ViewModel class for my search form so that I get the search parameters from View to controller. I can successfully get all my search parameters (which includes search query and check boxes if user wants to narrow search) in my controller class and retrieve data from database using repository pattern

var searchResult = _repository.GetItems(searchParms, chkbox1, chkbox2, ..., chkbox10)

After this, I am passing my searchResult to pagination helper like

var paginatedSearchResult = new PaginatedList<Items>(searchResult, page ?? 0, pageSize);

I am displaying the retrieved data in my view page

return View(paginatedSearchResult)

The problem, I am facing is, besides the data from the database, I also need to show the search query string and topic (for which checkbox was used) in my view page so that user can see what they searched for. I did not find any proper solution to this and had to use ViewBag. And now my Controller page looks ugly with more than 10 ViewBag. I know there must some good solution to this but I am not able to find it.Any suggestions will be highly appreciated.

Thanks, ACS

ACS
  • 443
  • 4
  • 15

3 Answers3

4

Direct answer to question's title is - alternative for viewbag in mvc is strongly typed ViewModel classes. Take a look at Create View Models

archil
  • 39,013
  • 7
  • 65
  • 82
2

You are soooo close already. You have a ViewModel you are using for the search - so you obviously know how to create a view model. Just create a new ViewModel that has your current ViewModel as a property and the database search viewmodel as a property.

public class SearchViewModel
{
    public YourExistingSearchOptionsViewModel SearchViewModel {get;set}
    public PaginatedList SearchResults {get;set;}
}

obviously that is very rough - but hopefully you get the idea there.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • Thanks @Adam. I tried creating new NewViewModel with 3 property inside. The first is `MyOriginalViewModel (that I used to retrieve search query)`, second - `PaginatedList (to retrieve paging)`, and the third `DBItems (that contains all the records)`. So instead of doing return `View(paginatedSearchResult)` , i am doing return `View(NewViewModel)`, and the following in my View Page. `@model IEnumerable`.But i got compilation error "The model item passed into the dictionary is of type 'NewViewModel', but requires a model item of type 'Sys.Coll.Generic.IEnumerable`". – ACS May 10 '11 at 23:04
  • You are returning a single object not a list as the list is inside the viewmodel. Remove the ienumerable<> and just use @model App.ViewModel.NewViewModel – Adam Tuliper May 11 '11 at 04:14
  • Well, if I remove the IEnumerable form view page, I can't loop through my records set. When I did that, I got the warning "foreach statement cannot operate on variables of type 'App.ViewModel.NewViewModel' because 'App.ViewModel.NewViewModel' does not contain a public definition for 'GetEnumerator'". I have many records that I want to loop through and show in view page besides the static user input. Thanks! – ACS May 11 '11 at 15:53
  • I got it working now. I used @model MyApp.ViewModel.NewViewModel in view page and declared a property 'public PaginatedList paginatedSearchList { get; set; }` in the NewViewModel. Thanks for your help. – ACS May 11 '11 at 20:34
  • thats what the code in the model above I added was for : ) public PaginatedList SearchResults {get;set;} – Adam Tuliper May 11 '11 at 22:09
0

I think you might be searching for the strongly typed "ViewData" property

kanchirk
  • 912
  • 2
  • 13
  • 29