I have a drop down list for departmentID that populates based on what is selected in DepartmentCategoryID, however I can;t get the validation working if it is left empty. It works or all others but this is done differently.
<div style="display: table-row;">
<div class="div-label-text-mandatory" , style="display: table-cell">
@Html.LabelFor(model => model.DepartmentCategoryID)
</div>
<div class="div-dropdown-menu" , style="display: table-cell">
@Html.DropDownListFor(model => model.DepartmentCategoryID (SelectList)ViewBag.DepartmentCategory, "Please select a Staff category", new { @id = "txtDepCatID", @onchange = "javascript:GetCity(this.value);" })
</div>
<div class="div-val-cell" , style="display: table-cell">
@Html.ValidationMessageFor(model => model.DepartmentCategoryID, "", new { @class = "text-danger" })
</div>
</div>
<div id="DepartmentDiv" style="display: none;">
<div class="div-label-text-mandatory" , style="display: table-cell"></div>
<div class="div-dropdown-menu" , style="display: table-cell">
@Html.LabelFor(model => model.DepartmentID):
<select id="txtDepartment" name="txtDepartmentID"></select>
</div>
<div class="div-val-cell" , style="display: table-cell">
@Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })
</div>
</div>
I tried adding a hidden for section i would set in jquery but this doesn't work either - not sure if hidden for can have missing validation?
<div style="display: table-row;">
<div class="div-label-text-mandatory" , style="display: table-cell"></div>
<div class="div-dropdown-menu" , style="display: table-cell">
@Html.HiddenFor(model => model.DepartmentID)
</div>
<div class="div-val-cell" , style="display: table-cell">
@Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })
</div>
</div>
Jquery to populate the list:
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script language="javascript" type="text/javascript">
function GetCity(_GetSubDepartment) {
var procemessage = "<option value='0'> Please wait...</option>";
$("#txtDepartment").html(procemessage).show();
var url = "@Url.Content("~/Employee/_GetSubDepartment")";
$.ajax({
url: url,
data: { DepartmentCategoryID: _GetSubDepartment },
cache: false,
type: "POST",
success: function (data) {
console.log("Data length: "+data.length)
if ((data.length) == 0) {
$('#DepartmentDiv').hide();
}
if ((data.length) > 0) {
var markup = "<option value='0'>Select department</option>";
for (var x = 0; x < data.length; x++) {
markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
$("#txtDepartment").html(markup).show();
//$('#DepartmentDiv').css('display', 'table-row').animate("slow");
$('#DepartmentDiv').css('display', 'table-row').show();
}
}
},
error: function (reponse) {
alert("error : " + reponse);
}
});
}
</script>
model
[DisplayName("Staff category")]
[Required(AllowEmptyStrings = false, ErrorMessage = " * is mandatory")]
public int DepartmentCategoryID { get; set; }
[DisplayName("Departments")]
[Required(AllowEmptyStrings = false, ErrorMessage = " * is mandatory")]
public int DepartmentID { get; set; }
controller:
[HttpPost]
public ActionResult _GetSubDepartment(int? DepartmentCategoryID)
{
ViewBag.Department = new SelectList(db.vwDimDepartments.Where(m => m.DepartmentCategoryID == DepartmentCategoryID).ToList(), "DepartmentID", "DepartmentName");
return Json(ViewBag.Department);
}
Is this because of the markup in Jquery to populate the list and that it is coming from a view bag?
Does anyone have a solution for this?