2016-08-09 6 views
0

"競合者"コンボボックスの選択した値を "xref"テキストボックスに添付された自動完成に渡す必要があります。オートコンプリートは最後のものが選択されていても、firtsアイテムの値だけを渡します。それを修正するには?jQueryオートコンプリート - 選択コンボボックス値

コントロール:

@Html.DropDownList("Competitor", (IEnumerable<SelectListItem>)ViewBag.Competitors, htmlAttributes: new { @class = "form-control" }) 
@Html.TextBox("xref", null, new { @class = "control-label col-lg-4 col-md-4 col-xs-4" }) 

オートコンプリートスクリプト:

$('#xref').autocomplete({ 
     source: '/Products/TagSearch?Competitor=' + $('#Competitor').val() 
    }); 

コントローラー:助けを

public ActionResult TagSearch(string term, int Competitor) 
    { 
     string[] tags = { }; 
     var myList = new List<string>(); 
     var xrefs = db.CrossReferences.Where(p => p.CompetitorId == Competitor).Where(p => p.Reference.StartsWith(term)); 
     if (xrefs != null) 
     { 
      foreach (var item in xrefs) 
      { 
       myList.Add(item.Reference.ToString()); 
      } 
      tags = myList.ToArray(); 
     } 
     return this.Json(tags.Where(t => t.StartsWith(term)), 
         JsonRequestBehavior.AllowGet); 
    } 

感謝。

答えて

0

解決策が見つかりました。キーアップ関数でラップするだけで十分です:)

$("#xref").keyup(function() { 
    $('#xref').autocomplete({ 
     source: '/Products/TagSearch?Competitor=' + $('#Competitor').val() 
    }); 
}); 
関連する問題