1

I want my MVC View send a type to action result as parameter so that i could use that type in my generic class

    public ActionResult ManageData(T genericType) 
    {
        var result = new BaseData<genericType>().GetSharedData();
        return View(result);
    }
Hamid Noahdi
  • 1,585
  • 2
  • 11
  • 18

1 Answers1

0

You can't have generic controller methods. I'm not sure whats your use-case but you could do something like this

public ActionResult ManageData(string type) 
{
    switch (type) {
        case 'first':
            return View(new BaseData<FirstType>().GetSharedData());
        case 'second':
            return View(new BaseData<SecondType>().GetSharedData());
        default:
            return View();
    }
}

Of couse you could improve this by moving your switch to some kind of BaseDataFactory or use string enums instead of pure strings for easier and more readable string -> type mapping.

zhuber
  • 5,364
  • 3
  • 30
  • 63
  • it is possible to have generic controllers and thus methods as well, see [answer1](https://stackoverflow.com/a/3418849/5519026), [answer2](https://stackoverflow.com/a/57746572/5519026) – LazZiya Sep 16 '19 at 06:27
  • 1
    Those are generic controllers that can be used as base classes and therefore generic type is resolved in compile time and that is not what he is asking for. You can't have generic method on which you pass generic argument dinamically from the client and as far as I understood, he wants that behaviour. – zhuber Sep 16 '19 at 07:32
  • @LazZiya Please see above. – zhuber Sep 16 '19 at 07:48