This doesn't appear to work when coming back from an AllowAddNew() call. The suggestion list remains open. I'm using EJ1 with MVC, version 16.3.0.29. Code looks like this Any suggestions on how to get the suggestion list to close up after the item gets added on the server? Thanks.
View
@{
Html.EJ().Autocomplete("Specialties")
.Datasource((IEnumerable<Specialty>) ViewData["Specialties"])
.AutocompleteFields(f => f.Text("Name").Key("Id"))
.Width("100%")
.MultiSelectMode(MultiSelectModeTypes.VisualMode)
.AllowAddNew(true)
.Value(Model.Specialties)
.ClientSideEvents(c => c.Select("onSpecialtySelect"))
.Readonly(isReadOnly)
.HtmlAttributes(new Dictionary<string, object> {{"name", "Specialties"}})
.Render();
}
Script
function onSpecialtySelect(args){
if(args.text.indexOf('Add New') >= 0){
var newval = args.text.replace('(Add New)','');
$.ajax({
url: '@Url.Action("AddSpecialty", "Contact")',
data: { specialtyName: newval.trim() },
type: 'POST',
dataType: "json",
success: function (response) {
var ac = $("#Specialties").ejAutocomplete("instance");
ac.model.dataSource.push({Name:response[0].Name,Id:response[0].Id});
ac._doneRemaining();
ac.suggestionList.css("display", "none");
}
});
}
}
Controller
[HttpPost]
public ActionResult AddSpecialty(Specialty model, string specialtyName)
{
var dbContext = new MyDbContext();
var specialty = new Specialty {Name = specialtyName };
dbContext.Specialties.Add(specialty);
dbContext.SaveChanges();
return Json(dbContext.Specialties.ToList());
}
Model
public class Specialty {
public int Id { get; set; }
public string Name { get; set; }
}