Am Using MVC4 and I am trying to implement cascading list boxes into one of my views, I've been following this tutorial and have successfully created 2 cascading drop down boxes but I now require the same type of functionality for list boxes where the user can select multiple items.
Just to clarify a user will be able to choose a Threat from a drop down list.
Upon selecting a Threat, a list box will be populated with the associated security events of the selected Threat, here the user will be able to select multiple security events or a single security event
Upon choosing their selected security events, another list box will be populated with any controls which are associated with the selected security events.
Here is what I have so far
View Model
public class AddNewRiskVM
{
public SelectList RiskTypeList { get; set; }
public SelectList GroupMembersList { get; set; }
public SelectList ThreatsList { get; set; }
public SelectList SecurityEventsList { get; set; }
public List<int> SelectedSecurityEventsIDs { get; set; }
public SelectList ISOControlList { get; set; }
public List<int> AssociatedIsoIDs { get; set; }
public int ISOControlID { get; set; }
public AddNewRiskVM()
{
SelectedSecurityEventsIDs = new List<int>();
AssociatedIsoIDs = new List<int>();
}
}
Controller
public void ConfigureNewRisk(AddNewRiskVM ViewModel)
{
var RiskTypes = _DBContext.RiskTypes;
var Threats = _DBContext.Threats;
ViewModel.RiskTypeList = new SelectList(RiskTypes, "ID", "Description");
ViewModel.GroupMembersList = new SelectList(new List<GroupMember>(), "ID", "Name");
ViewModel.ThreatsList = new SelectList(Threats, "ID", "Description");
ViewModel.SecurityEventsList = new SelectList(new List<SecurityEvent>(), "ID", "Description");
ViewModel.ISOControlList = new SelectList(new List<ISOControl>(), "ID", "Description");
}
public ActionResult AddNewRisk()
{
AddNewRiskVM ViewModel = new AddNewRiskVM();
ConfigureNewRisk(ViewModel);
return View(ViewModel);
}
public IEnumerable<ISOControl> GetISOControls(int SecurityEventID)
{
var QueryResults = _DBContext
.SecurityEventHasISOControls
.Include("SecurityEventHasISOControls.ISOControlID")
.Where(x => x.SecurityEventID == SecurityEventID)
.Select(x => x.ISOControl);
return QueryResults;
}
public JsonResult GetJsonISOControl(int ID)
{
var ISOControlListT = this.GetISOControls(Convert.ToInt32(ID));
var ISOControlList = ISOControlListT.Select(x => new SelectListItem()
{
Text = x.Description,
Value = x.ID.ToString(),
});
return Json(ISOControlList, JsonRequestBehavior.AllowGet);
}
Note - I have similar methods as GetISOControl & GetJsonControl which are used for The SecurityEvent
View
<div class="containeSelect">
@using (Html.BeginForm(null, null, new { @id = string.Empty }, FormMethod.Post, new { @id = "AddNewWithSelect" }))
{
<h3>Add New Risk</h3>
<fieldset>
@Html.DropDownList("RiskType", Model.RiskTypeList, "Select Risk Type", htmlAttributes: new { @class = "CascadeInputBox", onchange = "GetMembers()"})
@Html.DropDownList("GroupMember", Model.GroupMembersList, "Select Group Member", htmlAttributes: new {@class = "CascadeInputBox", @id = "CascadeDropDownList2"})
@Html.DropDownList("Threats", Model.ThreatsList, "Select Threat", htmlAttributes: new { @class = "CascadeInputBox", onchange = "GetSecurityEvents()"})
@Html.ListBoxFor(m => m.SelectedSecurityEventsIDs, Model.SecurityEventsList, htmlAttributes: new { @id = "SelectListEvent", onchange = "GetISOControls()"})
@Html.ListBox("ISOControls", Model.ISOControlList, htmlAttributes: new { @id = "SelectListISO"})
</fieldset>
}
<script>
function GetISOControls() {
$.ajax({
url: "@Url.Action("GetJsonISOControl", "RiskAssesment")",
dataType: "json",
type: "GET",
data: { id: $("#Events").val() },
error: function () {
},
beforeSend: function () {
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#SelectListISO").html(items);
}
});
}
</script>
<script>
function GetSecurityEvents() {
$.ajax({
url: "@Url.Action("GetJsonSecurityEvent", "RiskAssesment")",
dataType: "json",
type: "GET",
data: { id: $("#Threats").val() },
error: function () {
},
beforeSend: function () {
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#SelectListEvent").html(items);
}
});
}
</script>
This is the first time I've used ajax, I know I have to somehow change the line below, since in my helper method I haven't used a string name, instead I have used a linq expression, maybe that's all that is required for this to work, right now I cant see anything else but maybe am missing something?
data: { id: $("#Events").val() },
If anyone could offer any advice, especially with the ajax it would be appreciated.
Thanks in advance