0

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'

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
kmr
  • 15
  • 4

2 Answers2

1

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);
    }
}
MorenajeRD
  • 849
  • 1
  • 11
  • 27
1

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

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • Thanks for that. Fixed it but, still cannot pass "mymodel". – kmr Aug 05 '22 at 11:40
  • @kmr: It's because you don't need to save your model in the `TempData.` like you do: `TempData["Menu"] = mymodel;`. **This is wrong approach.** Just use `return RedirectToAction("Index", "Home", mymodel);`. If this will not work then problem in your `Index()` action method - add code of the `Index()` method to the question, please. the sentence _still cannot pass "mymodel"_ does not have useful information to understand your problem. – Jackdaw Aug 05 '22 at 11:54
  • @kmr: Then in the `index.cshtml` you can define the data model as `dymanic` . – Jackdaw Aug 05 '22 at 12:30