Aug 27, 2013, 8:15 AM
[CLOSED] Loading data from database to a combobox in Ext.Net
I tried to load the data from the database to the combo box. The combo box had to load the name of the cities from the database but the function
"GetCities" was not called from the index file.
here is the sample code
"GetCities" was not called from the index file.
here is the sample code
@(Html.X().ComboBox()
.ID("ComboBoxCity")
.TypeAhead(true)
.QueryMode(DataLoadMode.Local)
.ForceSelection(true)
.TriggerAction(TriggerAction.All)
.DisplayField("name")
.ValueField("id")
.EmptyText("Loading...")
.ValueNotFoundText("Loading...")
.Store(Html.X().Store()
.AutoLoad(false)
.Model(Html.X().Model()
.IDProperty("Id")
.Fields(
new ModelField("id", ModelFieldType.String) { Mapping = "Id" },
new ModelField("name", ModelFieldType.String) { Mapping = "Name" }
)
)
.Proxy(Html.X().AjaxProxy()
.Url(Url.Action("GetCities"))
.Reader(Html.X().JsonReader().Root("data"))
)
.Parameters(ps =>
ps.Add(new StoreParameter("country", "App.ComboBoxCountry.getValue()", ParameterMode.Raw))
)
.Listeners(ls =>
ls.Load.Handler = @"var combo = App.ComboBoxCity;
combo.setValue(records[0].get(combo.valueField));"
)
)
)
function at controllerpublic ActionResult GetCities(string country)
{
return this.Store(City.GetCities(country));
}
code at city class in Model
public static IEnumerable GetCities(string country)
{
List<object> data = new List<object>();
string conString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection sqlCon = new SqlConnection(conString);
SqlCommand sqlCmd = new SqlCommand("select Name from tblEmployee", sqlCon);
sqlCon.Open();
SqlDataReader dataReader = sqlCmd.ExecuteReader();
Employee ee = new Employee();
foreach (var v in dataReader)
{
ee.Name = v.ToString();
data.Add(ee);
}
return data;
}
Last edited by Daniil; Sep 03, 2013 at 5:05 AM.
Reason: Please use [CODE] tags, [CLOSED]