0

I've seen examples on here for saving one string to pass onto the next page. But, I haven't been able to find or figure out how to save multiple search parameters without requiring a variable for each search string, which is something I'd rather not do, as I have 12 search fields.

I've seen examples with saving your variables to a viewbag and then passing that when you go to the next page. That works for the one variable, but not for multiple.

Is there a more eloquent solution then to have to pass individual view bags for each variable?

Thanks!

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
Pinball125
  • 25
  • 1
  • 4
  • 1
    Why don't you pass an object in the viewbag with your said with variables as properties? ... Like a `Dictionary` key being search field, value being variable value. – interesting-name-here Aug 11 '17 at 19:47
  • It seems to me that the information should be part of the ViewModel. –  Aug 11 '17 at 20:33

2 Answers2

0

You can use a class for it like this:

public class SearchField
{
    public string Field1 { get; set; }
    public int Field2 { get; set; }
    // other fields
}

And save object to ViewBag like this:

SearchField objSearchField = new SearchField();
objSearchField.Field1="value1";
objSearchField.Field2 = 100;
// set other fields
ViewBag.SearchFields = objSearchField;
Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
0

You can use viewbag or tempdata["stringid"]. tempdata might be a better choice here. What you will do is create an array of strings (or a list might be even better!) and store all your information in that array. Then you store it on tempdata. Something like this:

 string[] URLs = new string[12];
 //do stuff
 Tempdata["URLs"] = URLs;

When you get to the page you need the URLs in again, either in the controller or in the view you will write:

string[] URLS = (string[])Tempdata["URLs"];
//do stuff

or 

string[] URLS = Tempdata["URLs"] as string[];

Something like that should work. You should read up on the other ways to pass data in ASP MVC:

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-to-pass-data-from-controller-to-view-in-mvc/

Passing an object array as TempData[] to view

Tyler S. Loeper
  • 816
  • 11
  • 22