I am selecting or Clicking on row of table, then relevant data from get populate in downdown and text field,
issue is that text field is getting populate ,but dropdown field is not getting populating ,i am sharing my whole code..
View
@model ERP_APP.Models.ItemModel
@{
ViewBag.Title = "CreaterItem";
}
<div class="col-lg-8">
<div class="card card-default mb-5">
<div class="card-header">Employee Form</div>
<div class="card-body">
@using (Html.BeginForm("CreateItem", "Item", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Descriptionitem, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Descriptionitem, new { htmlAttributes = new { @class = "form-control", @id = "item_Name" } })
@Html.ValidationMessageFor(model => model.Descriptionitem, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BaleSize, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BaleSize, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BaleSize, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.weight, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.weight, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.weight, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SecID, "Select Department Level", htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@* @Html.DropDownList("SecId", null, "Select Department", htmlAttributes: new { @class = "form-control", @id = "select2-1" })*@
@Html.DropDownList("SecId", (SelectList)ViewBag.secid, "Select", htmlAttributes: new { @class = "form-control", @id = "select2-1" })
@Html.ValidationMessageFor(model => model.SecID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IDUOM, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.IDUOM, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.IDUOM, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CID, "Select Category Level", htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.DropDownList("cID", null, "Select Department", htmlAttributes: new { @class = "form-control", @id = "select2-2" })
@Html.ValidationMessageFor(model => model.CID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<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>
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box box-primary">
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Item Name</th>
<th>Section</th>
<th>BaleSize</th>
<th>weight</th>
<th>UOM</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Itemss)
{
<tr>
<td>@Html.DisplayFor(module => item.CodeItem)</td>
<td>@Html.DisplayFor(module => item.Descriptionitem)</td>
<td>@Html.DisplayFor(module => item.Section)</td>
<td>@Html.DisplayFor(module => item.BaleSize)</td>
<td>@Html.DisplayFor(module => item.weight)</td>
<td>@Html.DisplayFor(module => item.UOM)</td>
</tr>
}
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<script src="~/Content/Template/vendor/ddlsearch.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#select2-3").on('change', function (e) {
$.ajax({
type: 'GET',
url: "/Item/GetItem",
dataType: 'json',
data: { codeitem: $(this).find('option:selected').val() },
success: function (response) {
$('#example1').hide();
$('#example1 tbody').empty();
$.each(response, function (i, item) {
var rows = "<tr>" +
"<td>" + item.CodeItem + "</td>" +
"<td>" + item.Descriptionitem + "</td>" +
"<td>" + item.Section + "</td>" +
"<td>" + item.BaleSize + "</td>" +
"<td>" + item.weight + "</td>" +
"<td>" + item.UOM + "</td>" +
"<td><a id='lnkView' href='javascript:void(0);'>View</a></td>" +
"</tr>";
$('#example1 tbody').append(rows);
});
$('#example1').show();
},
error: function () {
}
});
});
});
$(function () {
$.ajax({
type: 'GET',
url: "/Item/SelectItem",
dataType: 'json',
data: {},
success: function (response) {
$('#example1 tbody').empty();
$.each(response, function (i, item) {
var rows = "<tr>" +
"<td>" + item.CodeItem + "</td>" +
"<td>" + item.Descriptionitem + "</td>" +
"<td>" + item.Section + "</td>" +
"<td>" + item.BaleSize + "</td>" +
"<td>" + item.weight + "</td>" +
"<td>" + item.UOM + "</td>" +
"<td><a id='lnkView' href='javascript:void(0);'>View</a></td>" +
"</tr>";
$('#example1 tbody').append(rows);
});
$('#example1').show();
},
error: function () {
}
});
$('body').on('click', '#example1 tbody tr', function () {
$("#item_Name").val($(this).find('td').eq(1).html());
$("#select2-1").val($(this).find('td').eq(2).html());
});
});
</script>
Controller
using ERP_APP.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using DataBaseLayer;
namespace ERP_APP.Controllers
{
public class ItemController : Controller
{
SilverProductionEntities DB = new SilverProductionEntities();
// GET: Item
ItemModelDal sdb = new ItemModelDal();
public ActionResult CreateItem()
{
ModelState.Clear();
ItemModel item = new ItemModel();
//For Loading Data
item.Itemss = sdb.GetItem();
var cID = 0;
var secID = 0;
var codeitEM = 0;
ViewBag.cID = new SelectList(DB.Catagories.Where(bt => bt.CID > cID), "CID", "CName", "0");
ViewBag.SecId = new SelectList(DB.Sections.Where(bt => bt.SecID > secID), "SecID", "Secnam", "0");
ViewBag.codeitem = new SelectList(DB.ItemMasterFiles.Where(bt => bt.CodeItem > codeitEM), "codeitem", "Descriptionitem", "0");
return View(item);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateItem(ItemModel objmod)
{
try
{
int result = sdb.CreateItem(objmod);
if (result == 1)
{
ViewBag.Message = "Role Added Successfully";
ModelState.Clear();
}
else if (result == -1)
{
ViewBag.Message = "Role Already Exists!";
ModelState.Clear();
}
else
{
ViewBag.Message = "Unsucessfull";
ModelState.Clear();
}
return RedirectToAction("CreateItem");
}
catch
{
throw;
}
}
public JsonResult 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 I.Codeitem,I.Descriptionitem,S.Secnam Section,I.weight,I.BaleSize,U.NameUOM UOM FROM ItemMasterFile I inner join Sections S on S.Secid=I.SecId inner join UOM U on U.IDUOM=I.IDUOM where I.codeitem=@codeitem";
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"]),
Section = Convert.ToString(sdr["Section"]),
weight = Convert.ToInt32(sdr["weight"]),
BaleSize = Convert.ToString(sdr["BaleSize"]),
UOM = Convert.ToString(sdr["UOM"]),
});
}
}
con.Close();
}
}
var jsonResult = Json(items, JsonRequestBehavior.AllowGet);
return jsonResult;
}
public JsonResult SelectItem()
{
return Json(DB.ItemMasterFiles.Take(0).ToList(), JsonRequestBehavior.AllowGet);
}
}
}