I can't bind data to combobox, pls help me, it just show a blank combobox
My models
public class DanTocModels { [Key, Required, MaxLength(8)] public string MaDanToc{get;set;} public string TenDanToc { get; set; } }}
[controller] I used to Dapper(name of controller is: CommonController) and this below is my function
public IEnumerable get_all_dan_toc() { var rs = SqlMapper.Query(cn, "select * from tbl_dan_toc order by MaDanToc asc"); return rs; }
[view]
@model
@(Html.Infragistics().ComboFor(item => item.MaDanToc) .Width("270px") ..DataSource(Url.Action("/common/get_all_dan_toc")) .ValueKey("MaDanToc") .TextKey("TenDanToc") .DataBind() .Render() )
Hello Quang,
Thank you for your patience!
Please refer to the attached sample.
The sample demonstrates how to setup cascading combo using mvc with initial selected items.
Let me know if you need further assistance.
the combobox can't select by value if I I get json data like this
public ActionResult get_all_khu_vuc() { string strSQL = "select rtrim(MaKhuVucUT) as MaKhuVucUT, (rtrim(MaKhuVucUT) + ' | ' + TenKhuVucUT) as TenKhuVucUT from DM_TS_KhuVucUuTien order by MaKhuVucUT asc"; var data = SqlMapper.Query(this.cn, strSQL).ToList(); return this.Json(data, JsonRequestBehavior.AllowGet); }
it work If I use js file and declare
var data_khu_vuc = [ { "ma_khu_vuc": "01", "ten_khu_vuc": "01 | Khu vực 1" }, { "ma_khu_vuc": "02", "ten_khu_vuc": "02 | Khu vực 2" }, { "ma_khu_vuc": "03", "ten_khu_vuc" :"03 | Khu vực 3" } ];
You can select by value. For more refer to our api docs.
It's ok, I can set selected items in view by javascript here is my code:
$('#combo').igCombo({ initialSelectedItems: [{ index: 0 }] });
but my problem is I can't figure out how to set selected combobox with value (not index) from code behind in controller and return it to view.
Can I use combobox like this : List items = new List() and have selected if compare = true on value
foreach (countryModels)
items.Add(new SelectListItem { Text = ct.countryID.ToString().Trim() + " | " + ct.countryname.Trim() ,Value = ct.countryID.ToString().Trim() ,Selected = (ct.countryID.ToString().Trim() == CountryId) });
[controller]
ViewBag.List_Country=items;
Call combobox with have selected value from controller
Tk again
You can set combo mode to be dropdown list.
Then you can set initialSelectedItems.
For more refer to the attachment.