我是相当新的ASP.NET和C#,我有我的路由的一些问题。希望有人能帮助我。C#ASP.NET MVC 2基本的路由
用户应该给出3个参数(string
,bool
,bool
)。 所以,我有我的索引页上的一个小表格:
<% using (Html.BeginForm("search", "Home")) { %>
<label >Name: </label><br />
<input type="text" id='ml' name='ml' /><br />
<label >Sort members alphabethic? </label> <input type="checkbox" id='sortalph' name='sortalph' /><br />
<label >Number the list? </label><input type="checkbox" id='number' name='number' /><br />
<input type="submit" value='Submit'/>
<% } %>
Global.asax.cs
设置是这样的:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
routes.MapRoute(
"Search", // Route name
"{controller}/{action}/{ml}/{sortalph}/{number}", // URL with parameters
new { controller = "Docent", action = "Search" } // Parameter defaults
);
的Search
方法在我HomeController
开始看起来是这样的:
public ActionResult Search(string ml, bool? sortalph, bool? number)
{
if (sortalph == null)
{
sortalph = false;
}
if (number == null)
{
number = false;
}
当调试sortalph
和number
总是null
。我不知道为什么。
只是好奇:你为什么使用空值? –
@丹,我用nullables太频繁,这样我就可以缺少一个干净的URL /后与参数之间区分(因此使用默认,可能是“真”),当它实际上是通过故意“假”或' 0'而无需咨询价值提供商 –