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;
}
}
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?
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:
Get rip of the two switch in the controller and in the view
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)
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:
View:
Thanks, Stefan
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?
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:
controller:
The Index.cshtml view
The Time.cshtml view
The Overall.cshtml view
I think that would be a much nicer and better way.
is working on a reply...