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?

2
  • 1
    You check this? stackoverflow.com/questions/9817591/… Commented Nov 26, 2013 at 10:12
  • This is the trivial solution. reflection. I want to use the build-in mechanism to avoid from creating new code. Commented Nov 26, 2013 at 10:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.