2012-07-17 81 views
1

我需要一些幫助。我在MVC3中使用剃鬚刀視圖。我有一個自動完成功能的搜索欄,工作正常。現在根據需求。我需要在搜索文本框旁邊創建一個單選按鈕,並根據所選的單選按鈕值,我需要從不同的表中獲取自動完成文本。這是因爲,我的索引頁面視圖有3個不同的webgrid列表。因此,搜索應基於用戶想要搜索的內容進行操作,方法是將參數中的選項指定爲單選按鈕。將多個參數傳遞給自動完成返回方法

我有我的正常的jQuery代碼在這裏:

$(document).ready(function() { 
    $(":input[data-autocomplete]").each(function() { 
     $(this).autocomplete({ source: $(this).attr("data-autocomplete") }); 
    }) 
})* 

我修改了上面的傳遞第二個參數: -

$(document).ready(function() { 

    var radioval = $("#form0").find("input[type=radio]").attr("value"); 
    $(":input[data-autocomplete]").each(function (request) { 
     var srctxt = $(this).attr("value"); 
     $(this).autocomplete({ 
      source: "/Facility/FindNames/?term = " + $(this).attr("value") + "&stype = " + radioval 
     }); 
    }) 
}) 

我的目的是要通過第二個參數的搜索類型,是一個無線電按鈕組,然後在控制器下面根據傳遞的值更改查詢以從不同的表中進行選擇。

--controller方法

public JsonResult FindNames(string term, string stype) 
    { 

     string radioValue = null; 

     var result = _service.GetAllFacility() 
        .Where(r => r.FacilityName.Contains(term)) 
        .Take(10) 
        .Select(r => new { label = r.FacilityName }); 

     return Json(result, JsonRequestBehavior.AllowGet); 
    } 

但是S型的值總是來爲空。使用螢火蟲,我可以看到它確實有價值。有人能告訴我我的代碼有什麼問題嗎?實現這種搜索功能的最佳方式是什麼?

回答

2

你可以處理sourcefunction如下傳遞多個參數

var radioval = $("#form0").find("input[type=radio]").attr("value"); 
$(":input[data-autocomplete]").each(function() { 

    $this = $(this); 
    var srctxt = $this.val(); 
    $this.autocomplete({ 
      source: function (request, response) { 
      $.getJSON('/Facility/FindNames/', 
      { 
       stype: radioval, 
       term: srctxt 
      }, response); 
      } 
    }); 
}) 
+0

這工作,但有一個問題,你不停地打字不刷新自動完成搜索文本。你知道嗎? – rout0802 2012-07-18 14:44:05

+0

它看起來像srctxt值,控制器正在接收爲空。 – rout0802 2012-07-18 18:34:41

+0

這只是幫助我解決了ASP.NET Core中的一個問題。與Contoller中的以下行稍有區別.... return Json(result,JsonRequestBehavior.AllowGet); - >返回Json(result); – 2018-02-21 22:30:37