- Home
- Forum
- ASP.NET MVC
- How to get the selected item ID(KEY) from Autocomplete
How to get the selected item ID(KEY) from Autocomplete
Sorry about the inconvenience caused.
Currently we haven’t provided support to “Return the selected item’s key to controller in Autocomplete” and fix for this issue will available in our upcoming service pack release Volume 1,2015, which is expected to be rolled out at the end of this month. We will let you know once the service pack release has been rolled out. We appreciate your patience until then.
Hi Djamel,
We have prepared the sample based on get the selected item ID(key) value in controller side and please find the sample under the following location,
Sample: Autocomplete Sample
In the above sample, we have passed the selected item ID to controller in the button click event as shown below,
<code>
<script>
function OnClick(args)
{
var val = $("#selectcar").val();
var obj = $("#selectcar").data("ejAutocomplete");
var source= obj.option("dataSource");
for(var i=0;i<source.length;i++)
{
if (source[i].text == val) {
var id = source[i].uniqueKey;
break;
}
}
$.ajax({
type: "Post",
url: "result",
data: { "id": id },
success: function (result) {
alert(result)
},
});
}
</script>
</code>
In the controller page,you can get the id of the item as shown below code,
<code>
public ActionResult result(string id)
{
ViewBag.Message = id;
return PartialView("_Partial1");
}
</code>
Kindly let us know if you have further queries.
Regards,
Kasithangam
Thanks for contacting Syncfusion support,
We have checked with your requirement. We would like let you know that, we have provided your reported requirement in our latest releases (from 13.4.0.58 Volume 4, 2015 SP1). You can get the key of selected items in form post back by setting the mulitiSelect Mode as “visual mode“. Please refer the below code example,
|
@Html.EJ().Autocomplete("selectState").MultiSelectMode(MultiSelectModeTypes.VisualMode).Datasource((IEnumerable<object>)ViewBag.state).AutocompleteFields(f => f.Text("countryName").Key("index")) |
|
[HttpPost] public ActionResult GetData(string selectState) { return View("Index");// from selectState you can get the keys of selected items } |
If still you are facing any difficluites please provide more information about requirement along with your current EJ version which will help us to provide you the better solution,
Please check with the given solution and let us know if you have further queries,
Regards,
Sasikala Nagarajan
Hi Matthew,
Currently, we don’t have inbuilt functionality for when select the autocompelete item (not multiselect mode) and get the key of selected item in code behind. We have logged defect report for this. A support incident to track the status of this issue has been created under your account. Please log on to our support website to check for further updates.
https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents
Looking at what you said again i think this might be due to a bad translation "selected item's id" vs "selected items"
Can you please explain clearly, you are going to get the selected item id (key alone) or selected items (key and text) in the code behind?
Please let us know if you have any query.
Regards,
Kasithangam
Can you please explain clearly, you are going to get the selected item id (key alone) or selected items (key and text) in the code behind?
As said in the previous update, we have logged a defect report for the issue “when select the autocompelete item, can’t able to get the key (id) of selected item in code behind”.We will update you once the issue has been fixed. We appreciate your patience until then and also feel free to get back to us if you need any further assistance.
Regards,
Kasithangam
|
1.Using select event parameter
Cshtml
@Html.EJ().Autocomplete("selectcar").Width("205").Datasource((IEnumerable<CarsList>)ViewBag.datasource)
.AllowGrouping(false).HighlightSearch(false).AutocompleteFields(f => f.Text("text").Key("uniqueKey")).ClientSideEvents(e => e.Select("onselect"))
<script>
function onselect(e) {
keyvalue = e.key;
$.ajax({
type: 'POST',
url: '@Url.Action("AutoFeaturesnew", "Autocomplete")',
data: {
type: keyvalue
},
success: function (data) {
alert("key is suucessfully got in controller, Key:"+ data);
}
});
}
</script>
Controller
public JsonResult AutoFeaturesnew(string type)
{
return Json(type);
} |
|
2.Using selectValueByKey property
After selected a text, the selected item’s key value will be maintained in the selectvaluebykey property.
Cshtml
<form>
@Html.EJ().Autocomplete("selectauto").Width("205").Datasource((IEnumerable<CarsList>)ViewBag.datasource).MultiSelectMode(MultiSelectModeTypes.None).AllowGrouping(false).HighlightSearch(false).AutocompleteFields(e => e.Text("text").Key("uniqueKey"))
<br/>
<input id="submit" type="submit" value="Submit" />
</form>
<script>
$('form').submit(function () {
var ac = $("#selectauto").ejAutocomplete("instance");
$.ajax({
url: '/autocomplete/AutocompleteFeaturesnew',
data: { val: ac.model.selectValueByKey },
type: 'POST',
dataType: "json",
success: function (data) {
alert("key is suucessfully got in controller through form submit, Key:" + data);
}
});
}); </script>
Controller
public JsonResult AutocompleteFeaturesnew(string val)
{
return Json(val);
} |
- 13 Replies
- 8 Participants
-
DE Djamel eddine MACHOUK
- Apr 22, 2015 07:34 PM UTC
- Jan 10, 2017 11:36 AM UTC