C# 如何在 ASP.NET MVC 控制器中使用可选参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1119849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:39:58  来源:igfitidea点击:

How do I use Optional Parameters in an ASP.NET MVC Controller

c#asp.net-mvcc#-3.0

提问by Chris McKee

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).

所以它向同一页面发送查询(因为它唯一真正提供设置/更新适当下拉列表的替代方法,这是可以接受的,因为它将全部替换为 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.

所以Controller有如下方法。

public ActionResult Index(string country, int regions)

采纳答案by James S

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

该字符串应该没问题,因为它将作为空字符串传递。对于 int,使其可以为空:

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.:

请注意,ASP.NET 现在允许您定义默认参数。例如:

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.

但是,恕我直言,我建议您仅在具有语义意义的地方使用默认值。例如,如果 Regions 参数的目的是设置一个国家/地区的区域数量,并且大多数国家/地区有 2 个区域(北部和南部),那么设置默认值是有意义的。我不会使用表示缺少输入的“幻数”(例如,999 或 -1)——此时你应该只使用null.

回答by joelmdev

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 argumentif you wanted to avoid the potential pitfalls that come with nullable types. Your method signature would look like this:

我知道这已经很老了,但对于后代来说,重要的是要注意,从 C# 2010(又名 4.0,随 .NET 4 发布)开始,如果您想避免可空类型带来的潜在陷阱,您可以使用可选参数。您的方法签名如下所示:

public ActionResult Index(string Country, int Regions = -1)