0

I have a search method which takes in the searched string that the user inputs, and return a list of strings with the name that starts with the searched string. My method contains both boolean isSearch VARIABLE AND the search string itself No, it is not a duplicate since another question asks about the model while mine asked about individual values respectively. isSearch is a boolean variable not found in any model.

       public IActionResult Search(string search)
    {

        bool isSearch = true;

        var ssList = Database.SessionSynopses.Where(x => 
     x.SessionSynopsisName.StartsWith(search)).ToList();


        return View(isSearch, ssList);

        }

However, it throws me an error bool cannot be converted to string I have tried all ways to return an object to the views, including viewbag, but none of them has worked so far.

JApple
  • 19
  • 4
  • 3
    You create a view model containing those 2 properties (`bool isSearch` and `List`) and pass that to the view –  Feb 01 '18 at 11:10
  • Of course its a duplicate! And read the above comment! - pass one model to the view containing all the data your need! –  Feb 03 '18 at 01:46

2 Answers2

0

Use ViewBag, like this

ViewBag.Search = isSearch;

ViewBag.List= ssList;

And get these values on view in same way

@ViewBag.Search to get isSearch and @ViewBag.List to get ssList

Aamir Nakhwa
  • 393
  • 1
  • 12
  • 2
    I'd better use ViewBag only for secondary information, providing main data to the model. E.g. `ViewBag.Search = isSearch; return View(ssList);` – rattrapper Feb 01 '18 at 11:14
  • Better use a ViewModel instead of ViewBag. Properties in ViewBag must always be casted back to the original type, this is not necessary with ViewModel. Also, you can make a View require a certain ViewModel which can prevent some runtime errors. – Georg Patscheider Feb 01 '18 at 11:16
  • @GeorgPatscheider why? – cosh Feb 01 '18 at 11:17
  • @GeorgPatscheider yeah ViewModel is also a way to do it. But it's all up to you to pick any way to do so... – Aamir Nakhwa Feb 01 '18 at 11:18
0

You can return Tuple as,

Tuple<bool, List<CustomType>> tuple = new Tuple<bool, List>(boolValue, list);
return View(touple);

You also have to set the model type as Tuple<bool, List<CustomType>> inside your view for this.

PS: You cannot use Touple if your view is to edit the data, as mentioned by Stephen in comment.

Er Suman G
  • 581
  • 5
  • 12
Paritosh
  • 11,144
  • 5
  • 56
  • 74