0

I am learning ASP.NET MVC and it is easy to pass a single value to a view, using the following url format:

controller/view/id

and in the controller's code, I can simply define the integer id parameter as below:

ActionResult View(int? id)
{
}

but what is the accepted programming pattern (in ASP.NET MVC) to pass a range of values

controller/view/id1-id2

and how would the View method (in codebehind) would look like?

2 Answers 2

0

Firstly it's

controller/action/some-url-segment

Views are but the presentation logic that an action method with a view result returns, typically passing the view a model.

Secondly: There are no code-bind files for ASP.NET MVC Views, this is not web forms.

A possible route on the server side may be defined as follows:

url: "{controller}/{action}/page-{pagenum}"

And this would catch any url such as /admin/getuserlist/page-5

And an action method would receive those url segment params too.

ActionResult GetUserList(int pagenum)

The above gets ActionMethod parameter values from URL segments defined in the route.

You could also pass such values via query string:

/Admin/getuserlist?page=3&pagesize=45

You do not need to modify routes when using query string to pass values.

and a route such as url: {controller}/{action}/

Would happily pass that request to:

ActionResult GetUserList(int page, int pagesize)
1
  • Thanks for a detailed explanation - I can see the pattern now. Would have upvoted if I had enough reputation. Does that mean that I can have multiple method overloads for GetUserList method or I would have to leave just one?
    – Mitten
    Commented Apr 13, 2013 at 8:02
0

/Controller/Index?var1=1&var2=2&var3=3

ActionResult Index(int? var1, int? var2, int? var3)
{
    Return Content(
        String.Format("You entered: {0}, {1}, {2}", var1, var2, var3));
}

Result:

You entered: 1, 2, 3

/Controller/Index?values=1&values=2&values=3

ActionResult Index(IEnumerable<int> values)
{
    Return Content(
        String.Format("You entered: {0}, {1}, {2}", values[0], values[1], values[2]));
}

Result:

You entered: 1, 2, 3

I don't have any experience with code-behind, sorry. I'm curious, why do you use it?

3
  • Having multiple identically named query string parameters might be against convention I believe.
    – LaserBeak
    Commented Apr 12, 2013 at 9:21
  • @LaserBeak, what do you mean by that. Can you please clarify. Will it work, but just would be frowned upon by a developers (as it goes against convention), or MVC framework will fail to parse such quiery string or to map it to a collection class.
    – Mitten
    Commented Apr 13, 2013 at 8:06
  • @Rowan_Freeman Many thanks - didn't know that MVC allows to map a multiple parameters query string into a IEnumerable (@LaserBreak, I meant IEnumerable, not a collection in my previous comment)!
    – Mitten
    Commented Apr 13, 2013 at 8:09

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.