I am having an issue with Cascade DDL fetch and bind from the database into Controller - in EDIT view.
JSON function as below:
public JsonResult BindScenarioDDLByStudyId(string selectedStudyId)
{
var scenarioList = (from x in _repository.GetScenarioDDL()
where x.StudyId == selectedStudyId
select new
{
ScenarioId = x.ScenarioId,
ScenarioName = x.ScenarioDisplayId + " | " + x.ScenarioName
}).ToList();
return Json(scenarioList);
//return new JsonResult(scenarioList);
}
Jquery function:
function GetScenario(_studyId) {
var url = "/MappingStudy/BindScenarioDDLByStudyId/";
$.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].ScenarioId + ">" + data[x].ScenarioName + "</option>";
}
$("#ddlScenario").html(markup);
},
failure: function (response) {
alert("fail : " + response.responseText);
},
error: function (response) {
alert("error : " + response.responseText);
}
});
}
The JSON string (data) I am getting at line:
success: function (data) {
is as below:
0: {scenarioId: "01CKXHZPAEERP06F3CNKBAPE0F", scenarioName: "S1 | Minimal Investment"}
1: {scenarioId: "01EG17EX5J3DNZACT8S8NEX1SH", scenarioName: "S13 | Ning's 2nd scenario"}
2: {scenarioId: "01EG35TWG4WYW9YMPWJKMN7Y4N", scenarioName: "S14 | Testing 123"}
3: {scenarioId: "01EG3612HYMMVTVAHE0AQA5VHW", scenarioName: "S15 | sdss"}
4: {scenarioId: "01EGH2PPT6C3ANQECAHRFG0FNF", scenarioName: "S19 | Test 2"}
5: {scenarioId: "01CKXJDW47XMN0M03G2TVAEV7W", scenarioName: "S2 | Eastern Focus"}
6: {scenarioId: "01CKXKRDR6JGB78F8FGST65NJE", scenarioName: "S3 | Max Recovery"}
7: {scenarioId: "01CTNBW6CR5XNQAGQGDPB6WXS8", scenarioName: "S4 | Go Slow"}
8: {scenarioId: "01DFXEF1C6W8785R131XEZ7MQD", scenarioName: "S5 | Test"}
9: {scenarioId: "01DZRCZWNG0BTN3Q2XJWRA05HX", scenarioName: "S6 | Another scenario"}
10: {scenarioId: "01E1CP34572PKECNHK0E71VNED", scenarioName: "S7 | Test"}
11: {scenarioId: "01E1ECB0VRS1YWHR4HWN76FCZQ", scenarioName: "S8 | TEST TEST"}
i.e., small s - scenarioId and scenarioName
whereas, in Model/ViewModel properties are defined with capital s - ScenarioId and ScenarioName.
not sure if due to above reason, still not able to bind Scenario DDL selected value (from database) to Model -- back to Edit view.
View :
<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>
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; }
}
}
Repository class code:
IEnumerable<HdrFdpScenarios> GetScenarioDDL();
public IEnumerable<HdrFdpScenarios> GetScenarioDDL()
{
return context.HdrFdpScenarios;
}
IEnumerable<HdrPearlProject> GetProjectDDL();
public IEnumerable<HdrPearlProject> GetProjectDDL()
{
return context.HdrPearlProject;
}
What am I doing wrong?
Thank you in advance.