I Have a Dynamic Dropdown list populated with Data from Database like
for(int i=0;i<count;i++)
{
@Html.DropDownListFor(m => m.GetTimeSheetDetails[i].PROJ_ID,
(SelectList)ViewBag.ProjectList,
"-- Choose a Project --",
new { @class = "ddlProjectvalue" })
}
I have populated data by writing method like
public SelectList getProjects()
{
IEnumerable<SelectListItem> projectslist = (from proj in res.PROJECTs
where proj.IS_DELETED == "N"
select proj
).AsEnumerable()
.Select(projt => new SelectListItem()
{
Text = projt.NAME,
Value = projt.ID.ToString()
});
return new SelectList(projectslist, "Value", "Text", PROJ_ID);
}
and I have called in contoller and stored in ViewBag.Projects like
ViewBag.ProjectList = timesheetModel.getProjects();
// Ex: Contain list like item1,item2,item3
Now I want in Dynamic Dropdown list like if we select a item from First Dropdowlist should not show in Second Dropdown list(Ex. if we selected item1, should not show item1 in second Dropdown list), and next in third dropdown list should not show in both. How can I do that in Asp.net MVC? Please help me, i am not getting any ideas.
///Edited
I Have Tried with Script like
<script>
$(document).ready(function () {
$('.ddlProjectvalue').change(function () {
var selectedValue = $(this).val();
if (selectedValue != null && selectedValue != '') {
debugger;
@* location.href = '@Url.Action("GetDDLData", "Employer")?selectedValue=' + selectedValue;*@
$.getJSON('@Url.Action("/Employer/GetDDLData")', { selectedValue: selectedValue }, function (jsondata) {
var dllSecond = $('.ddlProjectvalue');
dllSecond.empty();
alert(selectedValue);
$.each(jsondata, function (index, data) {
dllSecond.append($('<option/>', {
value: data.value,
text: data.text
}));
});
});
}
});
});
</script>
and I Have added Json Result in Employer Controller
public ActionResult GetDDLData(string selectedValue)
{
int projectid = Convert.ToInt32(selectedValue);
IEnumerable<SelectListItem> projectslist = (from proj in db.PROJECTs where proj.IS_DELETED == "N" && proj.ID != projectid select proj).AsEnumerable().Select(projt => new SelectListItem() { Text = projt.NAME, Value = projt.ID.ToString() });
var result= new SelectList(projectslist, "Value", "Text", tm.PROJ_ID);
return Json(result,JsonRequestBehavior.AllowGet);
}
Still Not Getting it, where do am Wrong?