46

I have a set of drop down controls on a view that are linked to two lists.

//control
ViewData["Countries"] = new SelectList(x.getCountries().ToList(), "key","value",country);
ViewData["Regions"] = new SelectList(x.getRegions(country).ToList(), "id", "name", regions);

/*
on the view
*/

<% using (Html.BeginForm("","", FormMethod.Get))
           { %>
           <ol>
               <li>
                <%= MvcEasyA.Helpers.LabelHelper.Label("Country", "Country:")%>
                <%= Html.DropDownList("Country", ViewData["Countries"] as SelectList) %>
                <input type="submit" value="countryGO" class="ddabtns" />
               </li>
               <li>
                <%= MvcEasyA.Helpers.LabelHelper.Label("Regions", "Regions:")%>
                <%= Html.DropDownList("Regions", ViewData["Regions"] as SelectList,"-- Select One --") %>
                <input type="submit" value="regionsGO" class="ddabtns" />
               </li>
           </ol>     
                <br />
                <input type="submit" value="go" />
<% } %>

So its sending a query to the same page (as its only really there to provide an alternative way of setting/updating the appropriate dropdowns, this is acceptable as it will all be replaced with javascript).

The url on clicking is something like...

http://localhost:1689/?country=FRA&regions=117

Regions is dependent on the country code.

I'm trying to achieve this bit without bothering with routing as there's no real point with regards this function.

So the Controller has the following method.

public ActionResult Index(string country, int regions)

2 Answers 2

87

The string should be ok, as it'll get passed as an empty string. For int, make it nullable:

public ActionResult Index(string Country, int? Regions)

Also, you'll note I capitalized it in the same was as your querystring.

Edit

Note that ASP.NET now allows you to define default parameters. E.g.:

public ActionResult Index(string Country, int Regions = 2)

However, IMHO I'd recommend that you only use the default where it makes semantic sense. For example, if the purpose of the Regions parameter were to set the # of regions in a country, and most countries have 2 regions (North and South) then setting a default makes sense. I wouldn't use a "magic number" that signifies a lack of input (e.g., 999 or -1) -- at that point you should just use null.

1
  • How would one assign a default when using a View Model? Dec 13, 2018 at 9:27
34

I know this is quite old, but for posterity it's important to note that as of C# 2010 (aka 4.0, released with .NET 4) you could use an optional argument if you wanted to avoid the potential pitfalls that come with nullable types. Your method signature would look like this:

public ActionResult Index(string Country, int Regions = -1)
3
  • 4
    @ChrisMcKee I'm well aware that C# 4 wasn't available at the time of your question, but searches still bring people to these questions despite them being years old- that's how I stumbled on it. It's important to keep the information provided up to date unless the question is explicitly specific to a certain technology. In the case of your question, it reads the same now as it did 4 years ago, only now there are better solutions.
    – joelmdev
    Aug 30, 2013 at 13:07
  • 1
    I don't use c# any longer, and I can't / won't comment as to if this is the current best practice (you're essentially replacing null with -1, which has always felt more brittle to me... but I'm not here to argue / debate). However, I fully agree that since many people still come across this question whatever the best practice is should be available. I'd prefer to edit my original answer and link to a new asked/answered question.
    – James S
    Sep 1, 2013 at 19:15
  • @JamesS ScottGu talks about his preference here: weblogs.asp.net/scottgu/… which is the int Regions = -1 approach. Aug 15, 2016 at 7:03

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.