2014-05-09 115 views
-1

我有以下要求: - 有一個data_autocomplete_source傳遞額外的參數給我data_autocomplete_source

1.A文本框,如下: -

@ Html.TextBoxFor(型號=>模式。 Technology.Search,新{ data_autocomplete_source = Url.Action(「自動完成」,「開關」)})

2.兩單選按鈕來選擇究竟是什麼做的自動完成搜索關鍵字,按名稱或號碼如下: -

<input type="radio" name="searchType" value="number" /> number 
<input type="radio" name="searchType" value="name"/>name 

所以我的問題是否如何通過單選按鈕選擇,以及自動完成搜索詞?

我的自動完成腳本看起來如下: -

$("input[data-autocomplete-source]").each(function() { 
     var target = $(this); 
     target.autocomplete({ source: target.attr("data-autocomplete-source"), minLength: 2, delay: 2000 }); 

    }); 

編輯 我試過如下: -

@Html.TextBoxFor(model => model.Technology.Search, new { data_autocomplete_source = Url.Action("AutoComplete", "Switch", new { SearchBy = Model.SearchBy}) }) 
@Html.RadioButtonFor(a=>a.SearchBy, "number", new { id = "number" })number 
@Html.RadioButtonFor(a=>a.SearchBy, "Name", new { id = "name" })Name 

但SearchBy值將始終爲空?

感謝

回答

1

您需要將不從,這就是爲什麼它是null模型來的值。我可以建議你在你的JQuery函數中傳遞參數。 然後它可以用客戶端的參數調用你的Action。

$("#myTextBoxSearch").each(function() { 
    var searchByVal = $('input[name=SearchBy]:checked').val(); 
    var target = $(this); 
    target.autocomplete({ source:@Url.Action("AutoComplete", "Switch", new { SearchBy = searchByVal}) , minLength: 2, delay: 2000 }); 

}); 

如果你有你的觀點:

@Html.TextBoxFor(model => model.Technology.Search, new { id="myTextBoxSearch" }) 
@Html.RadioButtonFor(a=>a.SearchBy, "number", new { id = "number" })number 
@Html.RadioButtonFor(a=>a.SearchBy, "Name", new { id = "name" })Name 

注意,您可以使用您想要的變量的名稱。我只是告訴你一個方法。 我希望它能幫助你。