0

わかりました、必要なデータを正しく返すために必要なものはすべて揃っていると思いますが、それを返す方法がわかりません。

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);
}
4

2 に答える 2

0

選択リストでグループ化するには、何かカスタムを作成する必要があります。この拡張機能をチェックしてください:

ドロップダウンでの optgroup のサポート

そこから、コントローラを使用してビューをそのままフィードできます。使用することに決めた拡張機能に置き換えるだけです。

コントローラ:

ViewData["Manager"] = new SelectList(db.GetAllManagers(), "Manager", "Manager");

意見:

@Html.DropDownGroupList("ManagerDescription",(SelectList)ViewData["Manager"])
于 2013-05-30T18:59:12.967 に答える
0

私が使用しているコントローラーは次のとおりです。

public ActionResult GetOwners(int id = 0)
        {
            Departments department = db.Department.Find(id);

            AD_DepartmentProfile dp = new ActiveDirectory().GetDepartmentInfo(department.name, department.owner);

            return this.Json(dp, JsonRequestBehavior.AllowGet);
        }

そして、これがjQueryに加えた変更です。

function GetOwnerList(departmentid, departmentText) {
    jQuery('#owner').find('option').remove();

    jQuery.getJSON("/MasterList/GetOwners/" + departmentid, null, function (data) {
        var html;
        var len = data.AD_UserProfile.length;
        console.log(data);
        if (data.AD_ManagerProfile.name != null) {
            html += '<optgroup label="Manager">';
            html += '<option value = "' + data.AD_ManagerProfile.username + '">' + data.AD_ManagerProfile.name + '</option>';
            html += '</optgroup>';
        }

        if (len != 0) {
            html += '<optgroup label="Members">';
            for (var i = 0; i < len; i++) {
                html += '<option value = "' + data.AD_UserProfile[i].username + '">' + data.AD_UserProfile[i].name + '</option>';
            }
            html += '</optgroup>';
        }

        jQuery('#owner').append(html);
        jQuery('#owner').trigger('liszt:updated');
    });
}
于 2013-05-30T19:07:08.437 に答える