2

I'm looking for a way to convert a mvc4 model to querystring. The built-in mechanism of mvc4 is allowing me to do something like this:

@Url.Action("SearchWithQueryString","Search", new {@Title = "Title", @Author= " Author", @Date = "date"})

The result of this command is:

Url/Search/SearchWithQueryString?Title=title&Author=author&date=date

My goal is to pass a poco model and get the same result. for example, if I have this class:

public class Test
{
    public string Title {get;set;}
    public string Author {get;set;}
    public string Date {get;set;}
}

I want to be able to do something like this with using the built-in mechanism:

@Url.Action("SearchWithQueryString","Search", new Test())

and get the same result as I got previously.

Any ideas?

Aritra B
  • 1,726
  • 6
  • 29
  • 45
Dvir
  • 3,287
  • 1
  • 21
  • 33

1 Answers1

4

You should use the RouteValueDictionary class. This allows you to convert a model to a QueryString:

@Url.Action("SearchWithQueryString", "Search", new RouteValueDictionary(new Test()))

Where new Test() could also be Model for example.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104