Hi,
I need populate a DropDownList from values obtained from a database table using MySql database and view model using ASP NET MVC.
After populate a DropDownList I need filter WebGrid using the value selected on DropDownList.
But I have this error
CS1503: Argument 1: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable'
Help me to do it.
My code below
model
public List<SelectListItem> CityUO_List { get; set; }
public List<PersonModel> CityUO { get; set; }
controller
public static List<SelectListItem> PopulateDates(string UserUO)
{
List<SelectListItem> items = new List<SelectListItem>();
string cs = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(cs))
{
string sql = @String.Format(" SELECT City FROM `dotable`WHERE UOR = @UOR; ");
using (MySqlCommand cmd = new MySqlCommand(sql))
{
cmd.Parameters.AddWithValue("@UOR", System.Web.HttpContext.Current.Session["UserUO"]);
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = sdr["City"].ToString(),
Value = sdr["City"].ToString()
});
}
}
cmd.Connection.Close();
}
}
return items;
}
public ActionResult RecoveryAll()
{
PersonModel model = new PersonModel();
model.CityUO_List = PopulateDates(null);
return View(model);
}
[HttpPost]
public ActionResult RecoveryAll(PersonModel model)
{
model.CityUO_List = PopulateDates(???);
return View(model);
}
view
@model Ins.Models.PersonModel
@{
WebGrid webGrid = new WebGrid(source: Model.CityUO, canPage: true, canSort: false, rowsPerPage: 10);
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @Id = "WebGridForm" }))
{
@Html.DropDownList("City", Model.CityUO_List, "Select City", new { @id = "ddlCity" })
}