2012-01-12 20 views
2

希望有人可以幫忙,因爲我昨天晚上一直在爲我的大腦絞盡腦汁!基本上,我試圖讓一個搜索表單使用HTTPGET工作,所以我可以通過類似的URL可能檢索來自外部源的結果:使用HttpGet重定向循環MVC3搜索表格

http://url.com/Area/Controller/Action/SearchCategory/SearchCriteria

創建和傳遞的模型包含兩個屬性我的看法對於SearchCategorySearchCriteria並在視圖中具有關聯的HTML控件。如果我選擇一個類別併爲我的標準輸入一些內容,則此功能完美。但是,如果我沒有在我的標準中輸入任何內容,我將獲得無限重定向。我對這個特殊的特徵路線是這樣的:

context.MapRoute(
    "Dashboard-Search", 
    "Area/Controller/Action/{SearchCategory}/{SearchCriteria}", 
    new { 
     controller = "Controller", 
     action = "Action", 
     SearchCategory = "", 
     SearchCriteria = "" 
    } 
); 

我有我的模型實現IValidateableObject和驗證的東西已經進入了,但是很明顯的事情之前可以驗證該路由綁定完成。

任何想法???

路線

context.MapRoute(
    "Dashboard-Search-NoCriteria", 
    "HEP/Dashboard/Search/{category}", 
    new { controller = "Dashboard", action = "Search", category = "Case No" } 
); 

context.MapRoute(
    "Dashboard-Search", 
    "HEP/Dashboard/Search/{category}/{criteria}", 
    new { controller = "Dashboard", action = "Search", category = "Case No", criteria = "" } 
); 

控制器動作

[HttpGet] 
public ActionResult Search(string SearchCategory, string SearchCriteria) 
{ 
    // create new instance of model and add search criteria so entered 
    // data persists on post back 
    DashboardModel model = new DashboardModel() { 
     SearchCategory = SearchCategory, 
     SearchCriteria = SearchCriteria 
    }; 
    model.Search(SearchCategory, SearchCriteria); 

    // return the HTML view of another controller that displays the same list, 
    // only this time, the list is filtered according to GET data 
    return View("Overview", model); 
} 

HTML表單

@using (Html.BeginForm("Search", "Dashboard", FormMethod.Get)) { 

    @Html.LabelFor(m => m.SearchCategory, "Category:") 
    @Html.DropDownListFor(m => m.SearchCategory, new List<SelectListItem>() { 
     new SelectListItem() { Selected = true, Text = "Category", Value = "Category" } 
    }) 

    @Html.LabelFor(m => m.SearchCriteria, "Criteria:") 
    @Html.TextBoxFor(m => m.SearchCriteria) 

    <input type="submit" value="Search" class="button" /> 

} 
+0

我知道我可以通過使用客戶端驗證,以防止形式防止這一點沒有任何東西被張貼在** ** SearchCriteria但我關心的是什麼,如果用戶有某種原因禁用腳本攔截/ JavaScript的。我認爲一個無限循環看起來好像我沒有做正確的事情! – 2012-01-12 08:24:28

+2

你的意思是「不要在我的標準中輸入任何東西」? Criteria作爲GET參數傳遞還是作爲URL部分傳遞(您的路由代碼看起來像後面的選項)?如果條件作爲URL的一部分傳遞,則不指定它意味着您的URL與「Dashboard-Search」路由的模式不匹配,而是通過其他路由重定向。您能否提供更多關於您如何傳遞Criteria(示例URL?)併發布完整的'RegisterRoutes'功能代碼的信息? – 2012-01-12 08:32:27

+0

當提交時(使用標準中的某些內容)鍵/值對被放入查詢字符串OK中時,在

內的表單上確實存在2個輸入框。但是,如果我不輸入任何條件,則只有類別的鍵/值對被追加到查詢字符串中。我基本上是在阻止路由選擇,除非特別輸入了標準輸入。 – 2012-01-12 08:41:35

回答

0

你的路線,現在做不符合您的搜索表單。滿足您的Dashboard-Search路線

網址看起來應該像

http://example.com/HEP/Dashboard/Search/SampleCategory/SampleCriteria 

你又會產生一個GET請求的形式URL是這樣的:

http://example.com/HEP/Dashboard/Search/?Category=SampleCategory&Criteria=SampleCriteria 

最大的可能是你的方法Search(string SearchCategory, string SearchCriteria)沒有被調用 - 我建議您將請求重定向回只顯示搜索視圖的方法,而不實際執行任何搜索。

如果您需要更多的解釋或代碼,請顯示整個 RegisterRoutes方法。

0
[HttpGet] 
public ActionResult Search(string SearchCategory = null, string SearchCriteria = null) 

這將使你的控制器不考慮searchcategory或searchcriteria只要你在處理它們是空的情況下工作。

0

我會建議有一個單一的路線,然後設置您的默認值不同,通過使條件可選。

context.MapRoute(
    "Dashboard-Search", 
    "HEP/Dashboard/Search/{category}/{criteria}", 
    new { controller = "Dashboard", action = "Search", category = "Case No", criteria = UrlParameter.Optional });