Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • stevo 63 posts 106 karma points
    Jun 22, 2015 @ 20:28
    stevo
    0

    RenderMvcController or better SurfaceController

    Hello, I have a page which display data from an external database. I realized that with a RenderMvcController. I needed to display/sort the data e.g. "by category", "by time", "overall", ... I got it done BUT is there not a better solution... I was trying to work with surface controller and Html.ActionLink ...

    Here is my solution:

    Controller:

    public class RaceController : RenderMvcController
    {
        private readonly IRaceService _raceService;
    
        public RaceController(IRaceService raceService)
        {
            _raceService = raceService;
        }
    
        public override ActionResult Index(RenderModel model)
        {
            var view = Request.QueryString["v"] ?? "category";
            var raceId = CurrentPage.GetPropertyValue<int>("raceId");
            Race race = null;
    
            switch (view)
            {
                case "category":
                    race = _raceService.GetRaceByCategory(raceId);
                    break;
                case "gender":
                    race = _raceService.GetRaceByGender(raceId);
                    break;
                case "all":
                    race = _raceService.GetRaceByAllstar(raceId);
                    break;
                case "time":
                    race = _raceService.GetRaceByTime(raceId);
                    break;
                case "overall":
                    race = _raceService.GetRaceOverall(raceId);
                    break;
            }
    
            var raceModel = Mapper.Map<RaceViewModel>(race);
            return CurrentTemplate(raceModel);
        }
    }
    

    View:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>
    @{
        Layout = "Master.cshtml";
    }
    <div>
        <a href="?v=category">by category</a> |
        <a href="?v=time">by time</a> |
        <a href="?v=gender">by gender</a> |
        <a href="?v=allstar">allstar</a> |
        <a href="?v=overall">overall</a>
    </div>
    @{
        var view = Request.QueryString["v"] ?? "category";
    
        switch (view)
        {
            case "category":
            case "gender":
            case "allstar":
                Html.RenderPartial("_RaceResult", (RaceViewModel)Model);
                break;
            case "time":
                Html.RenderPartial("_RaceResultFlat", (RaceViewModel)Model);
                break;
            case "overall":
                Html.RenderPartial("_RaceResultOverall", (RaceViewModel)Model);
                break;
    
        }
    }
    

    Thanks, Stefan

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Jun 22, 2015 @ 21:35
    Nicholas Westby
    0

    What in particular are you hoping to do better?

    I see that you are apparently using different partials for each sort. Why not just sort the data in the controller and use a single partial to display it?

  • stevo 63 posts 106 karma points
    Jun 23, 2015 @ 04:51
    stevo
    0

    Hi Nicholas!

    Thanks you so much for your reply!

    I need different views because e.g. "by time" is a flat list of results and "by category" they is a nested loop. First over the categories and then a inner loop for all the results per category.

    My hope is:

    1. Get rip of the two switch in the controller and in the view
    2. Find a way to do it more like a pure ASP.NET MVC project, mines:

    controller:

    public class RaceController : Controller
    {
        private readonly IRaceService _raceService;
    
        public RaceController(IRaceService raceService)
        {
            _raceService = raceService;
        }   
    
        // GET: Race
        public ActionResult Index()
        {
            Race race = _raceService.GetRaceByCategory(raceId);     
            return View(Mapper.Map<RaceViewModel>(race));
        }
    
        public ActionResult Time()
        {     
            Race race = _raceService.GetRaceByCategory(raceId);     
            return View(Mapper.Map<RaceViewModel>(race));
        }
    
        public ActionResult Overall()
        {
            Race race = _raceService.GetRaceOverall(raceId);        
            return View(Mapper.Map<RaceViewModel>(race));
        }
    
        .
        .
        .    
    }
    

    The Index.cshtml view

    <h2>Races - by category</h2>
    @Html.Partial("_RaceNavi")
    @Html.Partial("_RaceResult", Model)
    

    The Time.cshtml view

    <h2>Races - by time</h2>
    @Html.Partial("_RaceNavi")
    @Html.Partial("_RaceResultFlat", Model)
    

    The Overall.cshtml view

    <h2>Race - Overall</h2>
    @Html.Partial("_RaceNavi")
    @Html.Partial("_RaceResultFlat", Model)
    

    I think that would be a much nicer and better way.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies