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
  • 4
  • 29
  • 44
Dvir
  • 3,137
  • 1
  • 20
  • 30
  • 1
    You check this? http://stackoverflow.com/questions/9817591/convert-querystring-from-to-object – Ilya Nov 26 '13 at 10:12
  • This is the trivial solution. reflection. I want to use the build-in mechanism to avoid from creating new code. – Dvir Nov 26 '13 at 10:16

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
  • 40,690
  • 11
  • 89
  • 103