Hi,
I have populate two DropDownList's from values obtained from a database table using MySql database and view model using ASP NET MVC.
After populate a DropDownList's I need filter WebGrid using the value selected on single DropDownList.
If selected value it's on first DropDownList the update WebGrid working correctly.
My problem is that the second DropDownList doesn't update the WebGrid.
How to do resolve this?
My code below
View
@using (Html.BeginForm("RecoveryAll", "Home", FormMethod.Post, new { @Id = "WebGridForm" }))
{
<div class="row">
<div class="col-md-3">
<div class="form-group">
@Html.DropDownList("Com", Model.ComUO_List, "Com", new { @id = "ddlcom" })
</div>
</div>
<div class="col-md-3">
<div class="form-group">
@Html.DropDownList("Tkt", Model.TktUO_List, "Tkt", new { @id = "ddltkt" })
</div>
</div>
</div>
}
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
columns: webGrid.Columns(
webGrid.Column("CustomerID", "Customer Id")))
<script type="text/javascript" src="~/Scripts/jquery.min.js"></script>
<script type="text/javascript">
$("body").on("change", "#ddlcom", function () {
$('#WebGridForm')[0].submit();
});
$("body").on("change", "#ddltkt", function () {
$('#WebGridForm')[0].submit();
});
$("body").on("click", ".Grid tfoot a", function () {
$('#WebGridForm').attr('action', $(this).attr('href')).submit();
return false;
});
</script>
controller
// GET: Home
public ActionResult RecoveryAll()
{
PersonModel model = PopulateModel(null, null);
return View(model);
}
[HttpPost]
public ActionResult RecoveryAll(string com, string tkt)
{
PersonModel model = PopulateModel(com, tkt);
return View(model);
}
private static PersonModel PopulateModel(string com, string tkt)
{
PersonModel model = new PersonModel()
{
ComUO = GetData(com),
ComUO_List = GetData("").Select(c => new SelectListItem { Text = c.Com, Value = c.Com }).Distinct().ToList(),
TktUO = GetData2(tkt),
TktUO_List = GetData2("").Select(c => new SelectListItem { Text = c.Tkt, Value = c.Tkt }).Distinct().ToList()
};
return model;
}
private static List<Customer> GetData2(string tkt)
{
List<Customer> customers = new List<Customer>();
string conString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
string sql = @String.Format(" SELECT * FROM `dotbl` WHERE Tkt = @Tkt OR @Tkt IS NULL; ");
using (MySqlConnection con = new MySqlConnection(conString))
{
MySqlCommand cmd = new MySqlCommand(sql);
if (!string.IsNullOrEmpty(tkt))
{
cmd.Parameters.AddWithValue("@Tkt", tkt);
}
else
{
cmd.Parameters.AddWithValue("@Tkt", (object)DBNull.Value);
}
cmd.Connection = con;
con.Open();
MySqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
customers.Add(new Customer
{
CustomerID = sdr["CustomerID"].ToString(),
});
}
con.Close();
}
return customers;
}
private static List<Customer> GetData(string com)
{
List<Customer> customers = new List<Customer>();
string conString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
string sql = @String.Format(" SELECT * FROM `dotbl` WHERE Com = @Com OR @Com IS NULL; ");
using (MySqlConnection con = new MySqlConnection(conString))
{
MySqlCommand cmd = new MySqlCommand(sql);
if (!string.IsNullOrEmpty(com))
{
cmd.Parameters.AddWithValue("@Com", com);
}
else
{
cmd.Parameters.AddWithValue("@Com", (object)DBNull.Value);
}
cmd.Connection = con;
con.Open();
MySqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
customers.Add(new Customer
{
CustomerID = sdr["CustomerID"].ToString(),
});
}
con.Close();
}
return customers;
}