Skip to content

Instantly share code, notes, and snippets.

@venblee
Last active December 18, 2015 20:59
Show Gist options
  • Save venblee/5843982 to your computer and use it in GitHub Desktop.
Save venblee/5843982 to your computer and use it in GitHub Desktop.
Retriecing Tags From DB and PreLoad them
public JsonResult SearchCity(string searchTerm)
{
var city = _locationService.SearchCity(searchTerm);
var result = city.Select(a => new { id = a.Id, text = a.City });
return Json(result, JsonRequestBehavior.AllowGet);
}
//RazorView
// For Model Binding use String , to return list.
// The Use to List<int>
@Html.TextBoxFor(m => m.Category , new {id="supplytype" , type="hidden" })
// Javascript
//////////////////////////////////
// TAGS FOR CATEGORIES
/////////////////////////////////
$("#supplytype").select2({
tags: true,
ajax: {
url: '@Url.Action("SearchCategory", "Suppliers")',
dataType: 'json',
data: function (term, page) {
return {
searchTerm: term
};
},
results: function (data, page) {
return {
results: data
};
}
}
});
/////////////////////////////////////////
// Preload your Tags from Database
/////////////////////////////////////////
var mytags;
$.ajax({
url: URL.GetSuppierTags,
dataType: "json",
data: ({ supplierid: $("#SupplierID").attr("value") }),
success: function (responsedata) {
$("#supplytype").select2("data", responsedata);
}
});
//////////////////////////////////////////////////
// Tags to List<int>
//////////////////////////////////////////////////
public List<SupplierCategoryMap> GetTags(string tags, int supplierid)
{
List<int> TagIds = new List<int>(tags.Split(',').Select(int.Parse));
var p = new List<SupplierCategoryMap>();
foreach (var tag in TagIds)
{
var s = new SupplierCategoryMap();
s.SupplierId = supplierid;
s.CategoryId = tag;
p.Add(s);
}
return p;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment