views:

52

answers:

2

I have a scenario where I'd like to render an ActionLink programmatically, ether from a controller or ideally an extension method. This is so I can wrap up some logic of wether to display the link and remove that logic from my view.

I can create an ActionLink using the MVC Futures project from CodePlex.

ActionLink actionLink = new ActionLink();
actionLink.ActionName = "MyActionName";
actionLink.ControllerName = "MyControllerName";

There is a RenderControl method that takes a HtmlTextWriter which I've been trying to use. I can't get this to work and a null reference exception is thrown each time within the Microsoft.Web.Mvc assembly (Futures).

[NullReferenceException: Object reference not set to an instance of an object.]
   Microsoft.Web.Mvc.Controls.ActionLink.Render(HtmlTextWriter writer) +643
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   MvcSpike.Controllers.HomeController.Index() in C:\...\Controllers\HomeController.cs:33
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +74
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314
   System.Web.Mvc.Controller.ExecuteCore() +105
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679150
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Any help would be great.

+1  A: 

Assuming you're in a controller, you could use the following:

string anchor = HtmlHelper.GenerateLink(
                    this.Request.RequestContext,
                    RouteTable.Routes,
                    "Link name",
                    "",
                    "MyActionName",
                    "MyControllerName",
                    null,
                    null);

But I'm not entirely sure what exactly you want to see in the end.

BuildStarted
Thanks, this works great. In short I just wanted the link built for me which I could put in my own html helper. In answer to Brian below, I had something like this in my views which I wanted to wrap up in something nicer http://gist.github.com/601732
James Antrobus
+1  A: 

Hey,

The best way would be store a flag in the model, that's set by the controller, whether to show the link or not, and show/hide it that way. Otherwise, you could look to create your own helper method with this logic; it's essentially rendering an tag, and using the UrlHelper class, the Action method, to generate the url. You could also use UrlHelper in the controller and pass the link through the model too, or in ViewData, etc.

HTH.

Brian