I did my best to create a test based on your unfinished code. As far as I can tell, the reason only 2 inputs are populated is because you did not write any code that sets the other input values?!
@model MvcBasic.Controllers.ItemModel
@{
ViewBag.Title = "CreaterItem";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<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>*@
@section scripts {
<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());
$("#BaleSize").val($(this).find('td').eq(3).text());
$("#weight").val($(this).find('td').eq(4).text());
$("#IDUOM").val($(this).find('td').eq(5).text());
$("#select2-3").val($(this).find('td').eq(0).text());
});
});
</script>
}
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcBasic.Controllers
{
public class ItemModel
{
public int weight { get; set; }
public string CodeItem { get; set; }
public string Descriptionitem { get; set; }
public string Section { get; set; }
public int BaleSize { get; set; }
public string UOM { get; set; }
public int SecID { get; set; }
public int IDUOM { get; set; }
public int CID { get; set; }
public List<Item> Itemss { get; set; }
}
public class Item {
public int weight { get; set; }
public string CodeItem { get; set; }
public string Descriptionitem { get; set; }
public string Section { get; set; }
public int BaleSize { get; set; }
public string UOM { get; set; }
}
public class ItemController : Controller
{
private List<Item> MockData;
private List<SelectListItem> cIDData;
private List<SelectListItem> SecIdData;
private List<SelectListItem> codeitemData;
private SelectList cID;
private SelectList SecId;
private SelectList codeitem;
public ItemController()
{
MockData = new List<Item>()
{
new Item()
{
BaleSize = 96,
CodeItem = "1",
Descriptionitem = "Description 1",
Section = "1",
UOM = "1",
weight = 10
},
new Item(){
BaleSize = 55,
CodeItem = "2",
Descriptionitem = "Description 1",
Section = "2",
UOM = "2",
weight = 20
},
new Item(){
BaleSize = 54,
CodeItem = "3",
Descriptionitem = "Description 3",
Section = "3",
UOM = "3",
weight = 40
},
};
cIDData = new List<SelectListItem>()
{
new SelectListItem()
{
Value ="1",
Text = "CID 1"
},
new SelectListItem()
{
Value ="2",
Text = "CID 2"
},
new SelectListItem()
{
Value ="3",
Text = "CID 3"
}
};
SecIdData = new List<SelectListItem>()
{
new SelectListItem()
{
Value ="1",
Text = "SecId 1"
},
new SelectListItem()
{
Value ="2",
Text = "SecId 2"
},
new SelectListItem()
{
Value ="3",
Text = "SecId 3"
}
};
codeitemData = new List<SelectListItem>()
{
new SelectListItem()
{
Value ="1",
Text = "codeitem 1"
},
new SelectListItem()
{
Value ="2",
Text = "codeitem 2"
},
new SelectListItem()
{
Value ="3",
Text = "codeitem 3"
}
};
cID = new SelectList(cIDData, "Value", "Text");
SecId = new SelectList(SecIdData, "Value", "Text");
codeitem = new SelectList(codeitemData, "Value", "Text");
}
// GET: Item
public ActionResult Index()
{
ItemModel item = new ItemModel();
ViewBag.cID = cID;
ViewBag.SecId = SecId;
ViewBag.codeitem = codeitem;
item.Itemss = MockData;
return View(item);
}
public ActionResult CreateItem()
{
ItemModel item = new ItemModel();
ViewBag.cID = cID;
ViewBag.SecId = SecId;
ViewBag.codeitem = codeitem;
item.Itemss = MockData;
return View(item);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateItem(ItemModel objmod)
{
if(!ModelState.IsValid)
{
return View(objmod);
}
return RedirectToAction("CreateItem");
}
public JsonResult GetItem(string codeitem)
{
var jsonResult = Json(MockData.Where(m => m.CodeItem == codeitem), JsonRequestBehavior.AllowGet);
return jsonResult;
}
public JsonResult SelectItem()
{
IEnumerable<Item> jasonResult = MockData.Take(1).ToList();
return Json(jasonResult, JsonRequestBehavior.AllowGet);
}
}
}