好吧我相信我有我需要的一切正確返回所需的數據,我只是不知道如何返回它。如何返回此數據並填充選擇元素
我有一個頁面帶有級聯下拉框,通過JQuery填充另一個框。我目前有jQuery().change()甚至編碼,我有模型編碼,我有選擇元素編碼。我只需要控制器的幫助。
下面是我希望HTML看起來(如果有更好或不同的方式,當然可以)。
AD_DepartmentProfile填充了部門經理和每個直接報告。我需要弄清楚如何將數據返回到Select元素。
<select id="owner" name="owner">
<optgroup label="Manager">
<option value="john">John Dow</option>
</optgroup>
<optgroup label="Members">
<option value="jane">Jane Doe</option>
<option value="josh">Josh Doe</option>
</optgroup>
</select>
這裏是我的模型:
public class AD_DepartmentProfile
{
public AD_DepartmentProfile()
{
this.AD_UserProfile = new HashSet<AD_UserProfile>();
this.AD_ManagerProfile = new AD_UserProfile();
}
public string name { get; set; }
public virtual AD_UserProfile AD_ManagerProfile { get; set; }
public virtual ICollection<AD_UserProfile> AD_UserProfile { get; set; }
}
public class AD_UserProfile
{
public string distinguishedName { get; set; }
public string email { get; set; }
public string manager { get; set; }
public string name { get; set; }
public string userPrincipalName { get; set; } // Useful if we need to allow a user to 'log on' as a different user.
public string userName { get; set; }
}
這裏是我的Jquery:
jQuery('#departmentID').change(function() {
var department = jQuery('#departmentID');
var value = department.val();
var text = department.children("option").filter(":selected").text();
GetOwnerList(value, text);
});
function GetOwnerList(departmentid, departmentText) {
jQuery('#owner').find('option').remove();
jQuery.getJSON("/MasterList/GetOwners/" + departmentid, null, function (data) {
var html;
var len = data.length;
for (var i = 0; i < len; i++) {
if (data[i] && data[i] != "") html += '<option value = "' + data[i].functionID + '">' + data[i].name + '</option>';
}
jQuery('#owner').append(html);
jQuery('#owner').trigger('liszt:updated');
});
這裏是控制器:
public ActionResult getOwners(int id = 0)
{
// Load up the department record
Department department = db.Department.Find(id);
// loads up the manager and direct reports into the model.
AD_DepartmentProfile dp = new ActiveDirectory().GetDepartmentInfo(department.name, department.owner);
}
我在等待迴應,並想出如何做到這一點,正在搞亂它。發表回答。 –
不錯。是的,我想我誤解了你的問題,並正在爲父母下拉。 –