Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:49:30+00:00 2026-05-27T02:49:30+00:00

I’m trying to generate an Html.ActionLink with the following viewmodel: public class SearchModel {

  • 0

I’m trying to generate an Html.ActionLink with the following viewmodel:

public class SearchModel
{
    public string KeyWords {get;set;}
    public IList<string> Categories {get;set;}
}

To generate my link I use the following call:

@Html.ActionLink("Index", "Search", Model)

Where Model is an instance of the SearchModel

The link generated is something like this:

http://www.test.com/search/index?keywords=bla&categories=System.Collections.Generic.List

Because it obviously is only calling the ToString method on every property.

What I would like to see generate is this:

http://www.test.com/search/index?keywords=bla&categories=Cat1&categories=Cat2

Is there any way I can achieve this by using Html.ActionLink

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:49:31+00:00Added an answer on May 27, 2026 at 2:49 am

    In MVC 3 you’re just out of luck because the route values are stored in a RouteValueDictionary that as the name implies uses a Dictionary internally which makes it not possible to have multiple values associated to a single key. The route values should probably be stored in a NameValueCollection to support the same behavior as the query string.

    However, if you can impose some constraints on the categories names and you’re able to support a query string in the format:

    http://www.test.com/search/index?keywords=bla&categories=Cat1|Cat2
    

    then you could theoretically plug it into Html.ActionLink since MVC uses TypeDescriptor which in turn is extensible at runtime. The following code is presented to demonstrate it’s possible, but I would not recommend it to be used, at least without further refactoring.

    Having said that, you would need to start by associating a custom type description provider:

    [TypeDescriptionProvider(typeof(SearchModelTypeDescriptionProvider))]
    public class SearchModel
    {
        public string KeyWords { get; set; }
        public IList<string> Categories { get; set; }
    }
    

    The implementation for the provider and the custom descriptor that overrides the property descriptor for the Categories property:

    class SearchModelTypeDescriptionProvider : TypeDescriptionProvider
    {
        public override ICustomTypeDescriptor GetTypeDescriptor(
            Type objectType, object instance)
        {
            var searchModel = instance as SearchModel;
            if (searchModel != null)
            {
                var properties = new List<PropertyDescriptor>();
    
                properties.Add(TypeDescriptor.CreateProperty(
                    objectType, "KeyWords", typeof(string)));
                properties.Add(new ListPropertyDescriptor("Categories"));
    
                return new SearchModelTypeDescriptor(properties.ToArray());
            }
            return base.GetTypeDescriptor(objectType, instance);
        }
    }
    class SearchModelTypeDescriptor : CustomTypeDescriptor
    {
        public SearchModelTypeDescriptor(PropertyDescriptor[] properties)
        {
            this.Properties = properties;
        }
        public PropertyDescriptor[] Properties { get; set; }
        public override PropertyDescriptorCollection GetProperties()
        {
            return new PropertyDescriptorCollection(this.Properties);
        }
    }
    

    Then we would need the custom property descriptor to be able to return a custom value in GetValue which is called internally by MVC:

    class ListPropertyDescriptor : PropertyDescriptor
    {
        public ListPropertyDescriptor(string name)
            : base(name, new Attribute[] { }) { }
    
        public override bool CanResetValue(object component)
        {
            return false;
        }
        public override Type ComponentType
        {
            get { throw new NotImplementedException(); }
        }
        public override object GetValue(object component)
        {
            var property = component.GetType().GetProperty(this.Name);
            var list = (IList<string>)property.GetValue(component, null);
            return string.Join("|", list);
        }
        public override bool IsReadOnly { get { return false; } }
        public override Type PropertyType
        {
            get { throw new NotImplementedException(); }
        }
        public override void ResetValue(object component) { }
        public override void SetValue(object component, object value) { }
        public override bool ShouldSerializeValue(object component)
        {
            throw new NotImplementedException();
        }
    }
    

    And finally to prove that it works a sample application that mimics the MVC route values creation:

    static void Main(string[] args)
    {
        var model = new SearchModel { KeyWords = "overengineering" };
    
        model.Categories = new List<string> { "1", "2", "3" };
    
        var properties = TypeDescriptor.GetProperties(model);
    
        var dictionary = new Dictionary<string, object>();
        foreach (PropertyDescriptor p in properties)
        {
            dictionary.Add(p.Name, p.GetValue(model));
        }
    
        // Prints: KeyWords, Categories
        Console.WriteLine(string.Join(", ", dictionary.Keys));
        // Prints: overengineering, 1|2|3
        Console.WriteLine(string.Join(", ", dictionary.Values));
    }
    

    Damn, this is probably the longest answer I ever give here at SO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.