Search code examples
asp.net-mvcasp.net-routing

RouteLink with dynamic parameters


I'm trying to store routes in database, and list those based on parameters.

Kinda like:

Route = "NewsDetail"

ParamKey 1 = "NewsID"

ParamVal 1 = "4"

.... 2..3

And I would like to know how I create those routeparameters for the link...

<%= Html.RouteLink("Somename", "NewsDetali", new { newsID = Model.News.NewsID, headline = Model.News.Headline })%>

is there some way to pass those parameters dynamicallly?

/M


Solution

  • Here's what you could do. As always start with a model:

    public class MyModel
    {
        public IDictionary<string, object> Values { get; set; }
    }
    

    Then in your controller action populate this model:

    public ActionResult Index()
    {
        var model = new MyModel
        {
            // TODO: Fetch those values from your database
            Values = new Dictionary<string, object>
            {
                { "NewsID", "4" },
                { "Headline", "foo bar" },
            }
        };
        return View(model);
    }
    

    And finally in your view:

    <%= Html.RouteLink("Somename", "NewsDetali", 
        new RouteValueDictionary(Model.Values)
    ) %>