How to submit Nothing as a route value to ASP MVC
Posted
by Adam
on Stack Overflow
See other posts from Stack Overflow
or by Adam
Published on 2010-06-10T08:58:54Z
Indexed on
2010/06/10
9:02 UTC
Read the original article
Hit count: 266
I have a route with several optional parameters. These are possible search terms in different fields. So, for example, if I have fields key, itemtype and text then I have in global.asax:
routes.MapRoute( _
"Search", _
"Admin.aspx/Search/{Key}/{ItemType}/{Text}", _
New With {.controller = "Admin", .action = "Search" .Key = Nothing, .ItemType = Nothing, .Text = Nothing} _
)
My action takes optional parameters:
Function Search(Optional ByVal Key As String = Nothing, _
Optional ByVal ItemType As Integer = 0, _
Optional ByVal Text As String = Nothing, _
Optional ByVal OtherText As String = Nothing)
It then checks if the Key and Text strings have a non-null (and non-empty) value and adds search terms to the db request as needed.
However, is it possible to send in a null value for, for example, Key but still send in a value for Text? If so, what does the URL look like? (Admin.aspx/Search//0/Foo doesn't work :) )
I know I can handle this using a parameter array instead, but wondered if this was possible using the sort of route described? I could of course define some other value as equivalent to null (for example, a space/%20) but is there any way to send a null value in the URL? I'm suspecting not, but thought I'd see if anyone knew of one.
I'm using ASP MVC 2 for this project.
© Stack Overflow or respective owner