I having trouble getting a value to my base controller. All I would like to do is have my base contoller pick up a ID from an ActionLink?
Link
<%= Html.ActionLink("About Us", "About", new { SectionId = 1 })%>
Base Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Website.Controllers
{
public class SectionController : Controller
{
//
// GET: /Section/
public SectionController(int SectionId)
{
if (SectionId == 1)
{
ViewData["Message"] = "GOT AN ID";
}
else
{
ViewData["Message"] = "NO ID";
}
}
}
}
Home Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Website.Controllers
{
[HandleError]
public class HomeController : SectionController
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
}
}
Solution so far
ActionLink
<%= Html.ActionLink("About Us", "About", new { SectionId = 1})%>
SectionAttribute.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Website.ActionFilters
{
public class SectionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Actions have sectionId parameter
object SectionId = filterContext.ActionParameters.FirstOrDefault(x => x.Key == "SectionId").Value;
if (SectionId != null && (int)SectionId == 1)
{
filterContext.Controller.ViewData["Message"] = "GOT AN ID";
}
else
{
filterContext.Controller.ViewData["Message"] = "NO ID";
}
}
}
}
SectionController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Website.ActionFilters;
namespace Website.Controllers
{
[Section]
public class SectionController : Controller
{
}
}
View
<%= Html.Encode(ViewData["Message"]) %>