Home > Software design >  How to pass ExpandoObject to another Controller?
How to pass ExpandoObject to another Controller?

Time:08-05

I'm new to ASP.NET Core MVC, and trying to create a simple website. So, now I'm trying to reach my navigation bar items from another controller.

This is my MenuController:

private static List<Menu> GetMenus()
{
        List<Menu> menu = new()
        {
            new Menu { MenuId = 1, MenuName = "Elektronik" },
            new Menu { MenuId = 2, MenuName = "Moda" },
            new Menu { MenuId = 3, MenuName = "Ev Tekstil" },
            new Menu { MenuId = 4, MenuName = "Outdooe" }
        };
        return menu;
}

public List<AltMenu> GetAltMenus()
{
    List<AltMenu> altMenus = new()
        {
            new AltMenu { AltMenuId = 1, AltMenuName = "Televizyon", AnaMenuId = 1 },
            new AltMenu { AltMenuId = 2, AltMenuName = "Giyim", AnaMenuId = 2 },
            new AltMenu { AltMenuId = 3, AltMenuName = "Alt Menu", AnaMenuId = 2 },
            new AltMenu { AltMenuId = 4, AltMenuName = "Alt Menu", AnaMenuId = 3 },
            new AltMenu { AltMenuId = 5, AltMenuName = "Alt Menu", AnaMenuId = 3 },
            new AltMenu { AltMenuId = 6, AltMenuName = "Alt Menu", AnaMenuId = 4 }
        };

    return altMenus;
}
    
public ExpandoObject GetAllMenu()
{
    dynamic mymodel = new ExpandoObject();
    mymodel.Menu = GetMenus();
    mymodel.AltMenu = GetAltMenus();

    TempData["Menu"] = mymodel;

    return RedirectToAction("Index", "Home", TempData["Menu"]);
}

Trying to reach this from HomeController (or another). So I can use my menu navbar in different pages without database.

PS.: I'm getting an error at the "return" line:

CS0029 Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.RedirectToActionResult' to 'System.Dynamic.ExpandoObject'

CodePudding user response:

there are 2 errors here, first one is how you are using the temp data:

you don't need to add it as a parameter, your redirect should look like this:

return RedirectToAction("Index", "Home");

to use it on the Home controller all you need to do is this:

if(TempData.ContainsKey("Menu"))
   string mymodel = TempData["Menu"]; 

Second is, you don't store objects on tempdata, if you want to do that you should consider some type of serialization, take a look a this example

this extension method was taken from the previously provided link:

public static class TempDataExtensions
{
    public static void Put<T>(this ITempDataDictionary tempData, string key, T value) where T : class
    {
        tempData[key] = JsonConvert.SerializeObject(value);
    }

    public static T Get<T>(this ITempDataDictionary tempData, string key) where T : class
    {
        object o;
        tempData.TryGetValue(key, out o);
        return o == null ? null : JsonConvert.DeserializeObject<T>((string)o);
    }
}

CodePudding user response:

The RedirectToAction() method returns the Microsoft.AspNetCore.Mvc.RedirectToActionResult object inherited from the ActionResult. Therefore you need to change your the public ExpandoObject GetAllMenu() method declaration to public IActionResult GetAllMenu().

See ControllerBase.RedirectToAction Method

  • Related