2014-09-26 65 views
0

嗨,很抱歉提出這樣一個簡單的問題,但我今天整天都在撓頭,無法弄清楚這一點。我發現了很多類似的問題,但沒有解決我的問題。MVC DropDownListFor如何添加RouteLink

我有一個頁面,其中包含產品清單和幾個按鈕,用於按類別過濾產品。由於產品數量增加,我決定將其更改爲下拉框。

所以我有下拉框,其中填充類:其中在觸發更改事件

@Html.DropDownListFor(m => m.SelectedCategoryId, Model.CategoryItems, new { id = "changeCategory" }) 

和javascript:

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#changeCategory").change(function() { 
     var selectedCategory = $(this).text(); 
     $.ajax({ 
      url: '@Url.Action("List", "Deal")', 
      type: 'GET', 
      data: { category: selectedCategory }, 
      cache: false, 
     }); 
    }); 
}); 
</script> 

這是行不通的。我以前的路由可與下面的代碼:

@foreach (var link in Model) { 
    @Html.RouteLink(link, new { 
     controller = "Deal", 
     action = "List", 
     category = link, 
     page = 1 
    }, new { 
    @class = "btn btn-block btn-default btn-lg" 
    }) 
} 

UPDATE:

我已經改變了jQuery代碼:

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#changeCategory").change(function() { 
     var selectedCategory = $("#changeCategory option:selected").text(); 
     $.ajax({ 
      url: selectedCategory, 
      type: 'POST', 
      cache: true, 
     }); 
    }); 
}); 
</script> 

和鏈接現在看起來正確的,但網站不會重新加載。當我在網絡部分的Chrome開發者工具中看到這個鏈接時,鏈接就會出現,當我點擊它時,它會打開正確的頁面。

爲什麼它不在網站上做到這一點?

更新2

我的控制器

public ViewResult List(string category, int page = 1) 
    { 
     DealsListViewModel model = new DealsListViewModel 
     { 
      Deals = repository.Deals 
      .Where(p => category == null || p.Category == category) 
      .OrderBy(p => p.DealID) 
      .Skip((page - 1) * PageSize) 
      .Take(PageSize), 
      PagingInfo = new PagingInfo 
      { 
       CurrentPage = page, 
       ItemsPerPage = PageSize, 
       TotalItems = repository.Deals.Count() 
      }, 
      CurrentCategory = category 
     }; 
     return View(model); 
    } 

任何幫助appriciated

+0

AJAX調用保持在同一頁面上。您是否嘗試重定向到另一個頁面,或者您是否嘗試將一些數據加載到當前頁面?請發佈你'交易'控制器的'List()'方法 – 2014-09-27 00:35:42

+0

我已經更新了這個問題。最好我想更新這個頁面上的數據,但如果這是不可能的,我會用你的答案來重定向頁面。非常感謝 – Whistler 2014-09-27 08:29:45

+0

您可以通過返回部分視圖更新當前頁面上的數據。我會盡快更新答案。 – 2014-09-27 08:32:44

回答

0

看來要重定向到List方法DealController並傳遞文字選定的選項。如果是的話

$("#changeCategory").change(function() { 
    window.location.href = '@Url.Action("List", "Deal")' + '/' + $(this).find('option:selected').text(); 
}); 

假設你的操作方法是一樣的東西

public ActionResult(string someValue) 

AJAX調用留在同一頁上,不重定向到另一頁。 出於好奇,爲什麼重寫默認的id(而不是僅僅使用$("#SelectedCategoryId").change(...)?

編輯

如果你想在AJAX成功函數

控制器

public PartialViewResult List(string category, int page = 1) 
{ 
    DealsListViewModel model = new DealsListViewModel .... 
    .... 
    return PartialView(model) 
} 

返回一些HTML,包括在頁面上,返回的局部視圖和更新頁面的html腳本(假設你有一個`id =「結果」的元素,你想渲染返回的HTML)

$("#changeCategory").change(function() { 
    var url = '@Url.Action("List", "Deal")'; 
    var category = $(this).find('option:selected').text(); 
    var page = ? // if you want to pass this as well 
    $.get(url, { category: category, page: page }, function(data) { 
    $('#results').html(data); 
    }); 
}); 
+0

謝謝,請看下面的問題的評論。 – Whistler 2014-09-27 08:30:28

+0

非常感謝,作品太棒了 – Whistler 2014-09-28 14:55:28

0

嘗試在你的Ajax調用以下:

type: 'POST' 
+0

我試過了,但結果相同 – Whistler 2014-09-26 21:53:17