簡體   English   中英

MVC DropDownList用於如何添加RouteLink

[英]MVC DropDownListFor how to add 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"
    })
}

更新:

我已將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);
    }

任何幫助都可以申請

在ajax調用中嘗試以下操作:

type: 'POST'

看來您想重定向到DealControllerList方法並傳遞所選選項的文本。 如果是這樣

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

假設您的操作方法類似於

public ActionResult(string someValue)

AJAX呼叫停留在同一頁面上,並且不會重定向到另一頁面。 出於好奇,為什么要覆蓋默認id (而不是僅使用$("#SelectedCategoryId").change(... )?

編輯

如果要返回頁面上要包含的html,請返回局部視圖並在AJAX成功函數中更新頁面html。

控制者

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

腳本(假設您有一個帶有`id =“ results”的元素,您想在其中呈現返回的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);
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM