将多个参数传递给自动完成返回方法

问题描述:

我需要一些帮助。我在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型的值总是来为空。使用萤火虫,我可以看到它确实有价值。有人能告诉我我的代码有什么问题吗?实现这种搜索功能的最佳方式是什么?

你可以处理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