locked
Populate a dropdownlist as an arguement to fill in by passing selected text on another dropdownlist. RRS feed

  • Question

  • User-1549556126 posted

    I have two dropdown lists one having list of domains another needs to be filled with group names & GUIDs (text & value), I am filling both of them by loading the function having shell instance and migrating those values into the dropdown using ViewBag when the page loads.

    I am trying to fetch the selected domain and pass it as an argument to the function to fill the groups that lies in only that selected domains. However, on the controller side I am not able to figure out what should be the initial step so far this is what I have done. 

    My C# Code:

     public ActionResult Group()
            {
                ViewBag.Message = "group";
                ViewBag.Domains = PowerShellExecutorLst(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Get-ADDomain.ps1", null, false);
    
                ViewBag.Groups = PowerShellExecutorLst(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Get-ADStewardGroups.ps1", User.Identity.Name.Split('\\')[1], true);
                return View();
            }
    
    public List<SelectListItem> PowerShellExecutorLst(string scriptPath, string arg, bool IsGroup, string domain=null)
            {
                //string outString = "";
                var shell = PowerShell.Create();
                var listItemStr = new List<String>();
                shell.Commands.AddCommand(scriptPath).AddArgument(arg).AddArgument(domain);
                var results = shell.Invoke();
                if (results.Any())
                {
                    foreach(var psObj in results)
                    {
    			
                        listItemStr.Add( Server.HtmlDecode(psObj.ToString().Trim('@', '{', '}').Replace("&#39;", "'")));
                    };
                }
                List<SelectListItem> listItems = listItemStr.Select(s => new SelectListItem {
                    Value = bifurcateString(s, true, IsGroup),
                    Text = bifurcateString(s, false, IsGroup)
                }).ToList();
                shell.Dispose();
                return listItems;
            }

    The IsGroup argument will determine whether the function is invoked for filling domain or group. I am not using any model as I am interacting directly with the AD DB using this function.  And then in my C# Code, I am loading ViewBag as a data source for the dropdown lists.

    <div class="row">
                <div class="col-md-12">
                    @Html.DropDownList("ddlDirectory", (IEnumerable<SelectListItem>)@ViewBag.Domains, "select domain", new { @class = "form-control" })
                </div>
            </div>
    
    <div class="row">
                <div class="col-md-12" style="width:auto">
                    @Html.DropDownList("ddlGroup", (IEnumerable<SelectListItem>)@ViewBag.Groups, "select group", new { @class = "form-control", @disabled = "disabled" })
                </div>
            </div>

    Is it really necessary to have ajax calls or the groups & GUIDs can be filled directly from Viewbag after selecting the domain?

    Tuesday, August 27, 2019 7:05 PM

Answers

  • User665608656 posted

    Hi vyasnikul,

    According to your description, I'm not sure what you need.

    Do you want to implement the cascade function of dropdownlist?

    When you choose different domains, the data source of the second dropdownlist changes to the corresponding groups?

    If so, I recommend that you use ajax to implement this function.

    If not, I hope you can describe your requirements in detail.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2019 9:41 AM
  • User665608656 posted

    Hi vyasnikul,

    As I mentioned in my last reply, I recommend that you use ajax to meet your needs.

    When the page loads, bind your first domain dropdownlist. When you select the first dropdownlist and switch, get the selected value as a parameter in jquery, pass it to the controller using ajax, and then in the controller, get the corresponding groups data source through your method, and transfer it in JSON format.

    Finally, go back to the JS side and bind the second groups dropdownlist.

    For more details, you can refer to this link : Cascading Dropdownlist using Ajax in Asp.Net Mvc 4

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 29, 2019 1:21 AM

All replies

  • User475983607 posted

    Is it really necessary to have ajax calls or the groups & GUIDs can be filled directly from Viewbag after selecting the domain?

    I'm not sure what you're asking.  There two ways to update a web page; full or partial refresh.  AJAX is a partial refresh.  Clicking a submit button or link is a full refresh.  It is up to you pick a method that best fits your needs.

    Tuesday, August 27, 2019 11:41 PM
  • User665608656 posted

    Hi vyasnikul,

    According to your description, I'm not sure what you need.

    Do you want to implement the cascade function of dropdownlist?

    When you choose different domains, the data source of the second dropdownlist changes to the corresponding groups?

    If so, I recommend that you use ajax to implement this function.

    If not, I hope you can describe your requirements in detail.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2019 9:41 AM
  • User-1549556126 posted

    @mgebhard I am actually trying to pass the value of one dropdown to fill values in to another. So, I guess you are right I gotta go with ajax call. I was just wondering if there would be any alternative.

    Wednesday, August 28, 2019 3:40 PM
  • User-1549556126 posted

    Yes @Yongqing this is exactly what I was wondering to do. Any suggestions in the code to optimize?

    Wednesday, August 28, 2019 3:41 PM
  • User665608656 posted

    Hi vyasnikul,

    As I mentioned in my last reply, I recommend that you use ajax to meet your needs.

    When the page loads, bind your first domain dropdownlist. When you select the first dropdownlist and switch, get the selected value as a parameter in jquery, pass it to the controller using ajax, and then in the controller, get the corresponding groups data source through your method, and transfer it in JSON format.

    Finally, go back to the JS side and bind the second groups dropdownlist.

    For more details, you can refer to this link : Cascading Dropdownlist using Ajax in Asp.Net Mvc 4

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 29, 2019 1:21 AM