Friday, 15 June 2012

asp.net mvc - Apply same query string paramter to all my links in ASP.MVC -


I am using query string to decide which database my MVC application should use

So I have created an application controller, which is a superclass for all my other controllers, every time it grabs this parameter and puts it in ViewBag.

But then all my links must be added to the parameter. It seems that there is a lot of work to manually add them manually:

  & lt; Li & gt; @ Html.ActionLink ("control type", "index", "control_type", new {server = ViewBag.ServerName}, faucet) & lt; / Li & gt;  

Is there a smart way to set it automatically for all generated links?

you HTMLHelper

  public static class ActionLinkExtension {public static MvcHtmlString CustomActionLink (It can write an extension for HtmlHelper htmlHelper, string link text, string actionName, string control name, object pathway, object HTMLAttributes). {Var root dictionary = new rootview (root value); // server = test-server Add linkDictionary.Add ("server", "test-server") to all action links; Return htmlHelper.ActionLink (linkText, actionName, control name, root-option, HtmlHelper.AnonymousObjectToHtmlAttributes (html content)); }}  

and use the extension method in the razor view

  & lt; Li & gt; @ Html.CustomActionLink ("control type", "index", "control_type", empty, blank) & lt; / Li & gt;  

No comments:

Post a Comment