I am developing an Asp.Net Core MVC web application.
One of the Razor views has three drop-down lists.
* The first drop-down list "Study DDL" is populated by the ViewModel data which is passed to the view.
* The secondary drop-down list "Scenario DDL" is populated using a JQuery and Ajax call based on the selected value from the first drop-down list (cascading).
* The third drop-down list "Project DDL" is a multi-select DDL.
View Code:
-------------
<select class="form-control col-md-8" onchange="GetScenario(this.value);"
asp-for="StudyId"
asp-items="@(new SelectList(@Model.StudyList, "StudyId", "StudyName"))">
<option value="">--Please Select--</option>
</select>
<select id="ddlScenario" class="form-control col-md-8" name="ddlScenario"
asp-for="ScenarioId">
<option value="">--Please Select--</option>
</select>
<select class="form-control col-md-8" multiple size="7"
asp-for="ProjectId1ToMany"
asp-items="@(new SelectList(@Model.ProjectList, "ProjectId", "ProjectName"))">
<option value="">--Please Select--</option>
</select>
Javascript/JQuery code:
---------------------------
function GetScenario(_studyId) {
var url = "/MappingStudy/GetScenarioByStudyId/";
$.ajax({
url: url,
data: { selectedStudyId: _studyId },
cache: false,
type: "POST",
success: function (data) {
var markup = "<option value=''>--Please Select--</option>";
for (var x = 0; x < data.length; x++) {
markup += "<option value=" + data[x].value + ">" + data[x].text + "</option>";
}
$("#ddlScenario").html(markup);
},
failure: function (response) {
alert("fail : " + response.responseText);
},
error: function (response) {
alert("error : " + response.responseText);
}
});
}
Repository Code:
-------------------
IEnumerable<HdrFdpScenarios> GetScenarioDDL();
public IEnumerable<HdrFdpScenarios> GetScenarioDDL()
{
return context.HdrFdpScenarios;
}
IEnumerable<HdrPearlProject> GetProjectDDL();
public IEnumerable<HdrPearlProject> GetProjectDDL()
{
return context.HdrPearlProject;
}
ViewModel class:
-----------------
namespace SLB.ETLWebApp.ViewModel
{
public class MappingStudyViewModel
{
public string StudyId { get; set; }
public List<HdrFdpStudy> StudyList { get; set; }
public string ScenarioId { get; set; }
public List<HdrFdpScenarios> ScenarioList { get; set; }
public string[] ProjectId1ToMany { get; set; }
public List<HdrPearlProject> ProjectList { get; set; }
public int Id { get; set; }
public int MappingId { get; set; }
public IEnumerable<MappingDetails> MappingDetails { get; set; }
public IEnumerable<Mapping> Mapping { get; set; }
}
}
Controller Code:
-----------------
public List<HdrFdpScenarios> BindScenarioDDL()
{
List<HdrFdpScenarios> scenarioList = new List<HdrFdpScenarios>();
scenarioList = _repository.GetScenarioDDL().ToList();
return scenarioList;
}
public JsonResult GetScenarioByStudyId(string selectedStudyId)
{
List<HdrFdpScenarios> scenarioList = new List<HdrFdpScenarios>();
scenarioList = BindScenarioDDL().Where(m => m.StudyId == selectedStudyId).ToList();
scenarioList.Select(x => new { ScenarioId = x.ScenarioId, ScenarioName = x.ScenarioDisplayId + " | " + x.ScenarioName }); //concatinate 2 columns data in DDL
var objScenarioList = new SelectList(scenarioList, "ScenarioId", "ScenarioName");
ViewBag.ScenarioList = objScenarioList;
return new JsonResult(objScenarioList);
}
public List<HdrPearlProject> BindProjectDDL()
{
List<HdrPearlProject> projectList = new List<HdrPearlProject>();
projectList = _repository.GetProjectDDL().ToList();
return projectList;
}
public string GetStudyIdByStudyName(string studyName)
{
var data = (from x in BindStudyDDL().ToList()
where x.StudyName == studyName
select x.StudyId).FirstOrDefault();
return data;
}
[HttpGet]
public IActionResult Upsert(int id, int mappingId)
{
MappingStudyViewModel mappingStudyViewModel = new MappingStudyViewModel();
mappingStudyViewModel.StudyList = BindStudyDDL();
mappingStudyViewModel.ProjectList = BindProjectDDL();
if (id == 0)
{
//Create
return View(mappingStudyViewModel);
}
else
{
//Update
var objMD = _repository.GetAllMappingDetails().Where(m => m.Id == id).FirstOrDefault();
var objM = _repository.GetAllMapping().Where(m => m.MappingId == mappingId).FirstOrDefault();
if (objM.MappingType == "1To1")
{
mappingStudyViewModel.Id = id;
mappingStudyViewModel.MappingId = mappingId;
mappingStudyViewModel.StudyId = GetStudyIdByStudyName(objMD.FdpStudy);
//mappingStudyViewModel.ScenarioList = BindScenarioDDL().Where(m => m.StudyId == mappingStudyViewModel.StudyId).ToList();
//mappingStudyViewModel.ScenarioList.Select(x => x.ScenarioDisplayId + " | " + x.ScenarioName);
//var objScenarioList = new SelectList(mappingStudyViewModel.ScenarioList, "ScenarioId", "ScenarioName");
//ViewBag.ScenarioList = objScenarioList;
//mappingStudyViewModel.ScenarioId = ViewBag.ScenarioList;
mappingStudyViewModel.ScenarioId = ?? // how to bind and fetch Scenario DDL value from DB, based on selecetd StudyId of Study DDL ?
}
else
{
mappingStudyViewModel.ProjectId1ToMany = ?? // how to bind multiple value from Database in multiselect dropdown ?
}
return View(mappingStudyViewModel);
}
}
I am facing 2 issues:
1. how to fetch and bind "Scenario DDL" value from DB, based on the selected value of "Study DDL"?
2. how to fetch and bind multiple values from Database in multi-select dropdown list?