I am trying to display data in table using dropdown, but it is not getting populating
Below is html view
DropDownList
<h4>Item Search</h4>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.CodeItem, "Select Item", htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.DropDownList("codeitem", null, "Select Item", htmlAttributes: new { @class = "form-control", @id = "select2-3" })
@Html.ValidationMessageFor(model => model.CodeItem, "", new { @class = "text-danger" })
</div>
</div>
table
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Roles List</h3>
</div>
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>CustomerId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Items)
{
<tr>
<td>@Html.DisplayFor(module => item.CodeItem)</td>
<td>@Html.DisplayFor(module => item.Descriptionitem)</td>
<td></td>
</tr>
}
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=codeitem]').change(function () {
var codeitem = $(this).find('option:selected').val();
if (codeitem != null && codeitem != '') {
$.getJSON('Item/GetItem', { country: codeitem }, function (response) {
if (response.length > 0) {
$("[id*=example1] tbody").empty();
var row;
$.each(response, function (index, item) {
row += "<tr><td>" + item.Codeitem + "</td><td>" + item.Descriptionitem + "</td></tr>"
})
$("[id*=example1] tbody").append(row);
}
});
}
});
});
</script>
Controller
public ActionResult GetItem(string codeitem)
{
List<ItemModel> items = new List<ItemModel>();
using (SqlConnection con = new SqlConnection("data source=SERVER1\\SQLEXPRESS;initial catalog=SilverProduction;integrated security=True;MultipleActiveResultSets=True;"))
{
string query = "SELECT Codeitem,Descriptionitem FROM ItemMasterFile";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Codeitem", codeitem);
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new ItemModel
{
CodeItem = Convert.ToInt32(sdr["CodeItem"]),
Descriptionitem = Convert.ToString(sdr["Descriptionitem"]),
});
}
}
con.Close();
}
}
return Json(items, JsonRequestBehavior.AllowGet);
}