如何在ASP中使用可选参数。净MVC控制器

时间:2021-05-30 11:13:00

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

单击的url类似于……

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 个解决方案

#1


70  

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

字符串应该是ok的,因为它将作为一个空字符串传递。对于int,使其为可空:

public ActionResult Index(string Country, int? Regions)

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

同样,你会注意到,我把它大写了,和你的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.

然而,IMHO建议您只使用默认的语义感。例如,如果区域参数的目的是设置一个国家的区域数,而大多数国家有两个区域(北部和南部),那么设置一个默认值是有意义的。我不会用一个“神奇的数字”来表示输入的缺乏(比如999或-1)——这时你应该使用null。

#2


26  

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. You're method signature would look like this:

我知道这很旧,但是对于后代来说,需要注意的是,在c# 2010(也就是使用。net 4发布的4.0版本)中,如果您想要避免可空类型的潜在陷阱,您可以使用一个可选的参数。你的方法签名是这样的:

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

#1


70  

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

字符串应该是ok的,因为它将作为一个空字符串传递。对于int,使其为可空:

public ActionResult Index(string Country, int? Regions)

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

同样,你会注意到,我把它大写了,和你的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.

然而,IMHO建议您只使用默认的语义感。例如,如果区域参数的目的是设置一个国家的区域数,而大多数国家有两个区域(北部和南部),那么设置一个默认值是有意义的。我不会用一个“神奇的数字”来表示输入的缺乏(比如999或-1)——这时你应该使用null。

#2


26  

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. You're method signature would look like this:

我知道这很旧,但是对于后代来说,需要注意的是,在c# 2010(也就是使用。net 4发布的4.0版本)中,如果您想要避免可空类型的潜在陷阱,您可以使用一个可选的参数。你的方法签名是这样的:

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