Select row in table and populate row value on Forms fields.
Below is View Code.
<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" } })
@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.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-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>@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>
<td></td>
</tr>
}
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.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>" +
"</tr>";
$('#example1 tbody').append(rows);
});
$('#example1').show();
},
error: function () {
}
});
});
});
</script>
Note: I have Dropdown populating using below code
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);
so how to populate dropdown, when selcting row with other field