In the end I used jquery to select the dropdown if the Id has a value.
In the view, I create a hidden field (don't use the @Html.HiddenFor)
@Html.Hidden("SelectedCategorySubId", Model.SelectedCategorySubId.GetValueOrDefault(), new { @id = "hdnSelectedCategorySubId" })
In JS file,in the call back method of ajax call (to load the drop down),after adding data to dropdown, get the hidden field value and then select that value in dropdown.
$.ajax({
type: 'POST',
url: "GetSubCategory",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(param),
success: function (subCategoryList) {
if (subCategoryList != null && subCategoryList != '') {
$("#SelectedCategorySubId").append('<option>--- Select ---</option>');
$.each(subCategoryList, function (i, subCategory) {
$("#SelectedCategorySubId").append($('<option/>', {
value: subCategory.Value,
text: subCategory.Text
}));
});
//Set selected value from hidden field
var selectedCategorySubId = $("#hdnSelectedCategorySubId").val();
// alert("Sub Cat" + selectedCategorySubId);
$("#SelectedCategorySubId").val(selectedCategorySubId);
}//end if
else {
//alert("empty subcategoryList " + subCategoryList);
$("#tdSubCategory").hide();
}
}
And update the hidden field if the dropdown changes
//Update hidden field, if cat changes
$("#SelectedCategorySubId").change(function () {
$("#hdnSelectedCategorySubId").val($(this).val());
});
I hope this helps someone.