わかりました、必要なデータを正しく返すために必要なものはすべて揃っていると思いますが、それを返す方法がわかりません。
JQueryを介して別のボックスにデータを入力するカスケードドロップダウンボックスのあるページがあります。現在、jQuery().change() もコード化されています。モデルもコード化されており、select 要素もコード化されています。コントローラーの助けが必要なだけです。
これが私がHTMLをどのように見せたいかです(もちろん、より良い方法または別の方法があればOKです)。
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);
}