Hi @akhter hussain ,
You can get CodeItem through the jQuery Onchange event, and then obtain Secid according to Linq query.
public JsonResult GetSections(int itemId)
{
int Secid = DB.ItemMasterFiles.First(a => a.CodeItem == itemId).SecID;
var STATUS_LIST = (from s in DB.Sections
where s.SecID ==Secid
select new SelectListItem()
{
Text = s.SecName,
Value = s.SecID.ToString(),
}).ToList();
return Json(STATUS_LIST, JsonRequestBehavior.AllowGet);
}
I wrote a demonstration, you can refer to:
@model MVC418.Models.CascadingModel
@{
ViewBag.Title = "Home Page";
}
<body>
<div class="col-lg-8">
<div class="card card-default mb-5">
<div class="card-header">Designation Form</div>
<div class="card-body">
@using (Html.BeginForm("index", "home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<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", (SelectList)ViewBag.Codeitem, "Select", htmlAttributes: new { @class = "form-control", @id = "select2-1" })
@Html.ValidationMessageFor(model => model.CodeItem, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SecId, "Select Section", htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
<div id="123">
@Html.DropDownList("SecId", null, "Select Section", htmlAttributes: new { @class = "form-control", @id = "select2-2" })
</div>
@Html.ValidationMessageFor(model => model.SecId, "", 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>
}
<div>
@Html.ActionLink("Back to List", "AllDesignation")
</div>
</div>
</div>
</div>
</body>
<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-1").change(function () {
var itemId = $("#select2-1").val();
if (itemId != "") {
GetSections(itemId);
}
else {
$("#select2-2").empty();
}
});
});
function GetSections(itemId) {
$.ajax({
async: true,
type: 'GET',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
url: '/Home/GetSections',
data: { itemId: itemId },
success: function (data) {
$('#select2-2').empty();
$(data).each(function (index, item) {
$('#select2-2').append($('<option/>', { value: item.Value, text: item.Text }))
})
},
error: function () {
alert("There is some problem to get.");
}
});
}
</script>
public ActionResult Index()
{
var secID = 0;
ViewBag.secID = new SelectList(DB.Sections.Where(bt => bt.SecID > secID), "SecID", "SecName", "0");
var codeItem = 0;
ViewBag.codeItem = new SelectList(DB.ItemMasterFiles.Where(bt => bt.CodeItem > codeItem), "Codeitem", "Descriptionitem", "0");
return View();
}
[HttpGet]
public JsonResult GetSections(int itemId)
{
int Secid = DB.ItemMasterFiles.First(a => a.CodeItem == itemId).SecID;
var STATUS_LIST = (from s in DB.Sections
where s.SecID ==Secid
select new SelectListItem()
{
Text = s.SecName,
Value = s.SecID.ToString(),
}).ToList();
return Json(STATUS_LIST, JsonRequestBehavior.AllowGet);
}
public class itemMasterFile
{
[Key]
public int SecID { get; set; }
public int CodeItem { get; set; }
public string Descriptionitem { get; set; }
}
public class Sections
{
[Key]
public int SecID { get; set; }
public string SecName { get; set; }
}
public class CascadingModel
{
public List<Sections> Sections { get; set; }
public List<itemMasterFile> ItemMasterFiles { get; set; }
public int? SecId { get; set; }
public int? CodeItem { get; set; }
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
I am using below view
Class
Controller