Quantcast
Channel: Developer to developer
Viewing all articles
Browse latest Browse all 9076

Multiple actions inside a single page controller

$
0
0

So I have been trying to do the following things

  1. Create a custom login for my website users
  2. After log in user fo to there dashboard something like dashboard/index
  3. Now I want to have sub views as follows
    1. dashboard/reports
    2. dashboard/singleReport (maybe I need a new page type for reports here)
    3. dashboard/someAction
    4. ...

The issue I am having is that should I create page types for each of these pages? I would not be adding content through the CMS into these views but the data would be coming from some external api.

It seems strange to create page types for just one page and also not adding data from cms. 

In MVC I was able to have one controller with multiple actions and that okay but here its not the case. I did wrote this code but it seems like wrong way of doing it

    public class DashboardPageController : PageControllerBase<DashboardPage>
    {
        public async Task<ActionResult> Index(DashboardPage currentPage)
        {
            /* Implementation of action. You can create your own view model class that you pass to the view or
             * you can pass the page type for simpler templates */
            MockRystadApi api = new MockRystadApi();
            var model = new DashboardPageModel(currentPage);
            foreach(var item in await api.GetReports())
            {
                model.Reports.Add(new DashboardPageModel.Report{
                    AvailableForModules = item.AvailableForModules,
                    SmallDescription = item.SmallDescription,
                    Title = item.Title,
                    Id = item.Id
                });
            }
            return View(model);
        }
        public ActionResult Report(DashboardPage currentPage, String id)
        {
            MockRystadApi api = new MockRystadApi();
            var model = new DashboardPageModel(currentPage);
            int reportId = int.Parse(id);
            var report = api.GetReport(reportId).Result;
            model.SingleReport.Id = report.Id;
            model.SingleReport.Title = report.Title;
            model.SingleReport.SmallDescription = report.SmallDescription;
            model.SingleReport.AvailableForModules = report.AvailableForModules;
            return View(model);
        }
    }

The issue with the above code is that both the actions require DashboardPageModel to be set. So I would have to fiddle with the model properties. Like when I am viewing all the reports I have the property SingleReport to be null and when I am showing overview of a single report all other data is null. 

Can I get some help here?


Viewing all articles
Browse latest Browse all 9076

Trending Articles