如何在MVC视图中单击按钮时使用控制器方法填充文本区域?

时间:2019-06-03 15:35:41

标签: asp.net-mvc razor model-view-controller active-directory

我创建了PowerShell脚本,以便在网页加载组名称时将其加载到下拉列表中,以获取活动目录组。我要尝试做的下一件事是使用搜索按钮click事件,通过传递PowerShell脚本将组详细信息加载到文本区域中,并将成员加载到表中。我正在使用库System.Management.Automation处理PowerShell脚本。当我直接使用PowerShell时,我不希望我的项目依赖于任何模型类。因此,我仅在控制器和视图之间进行交互。

控制器代码:

using System.Web.Mvc;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.Linq;
using System;

namespace MyProject.UI.MVC5.Controllers
{
    public class GroupController : Controller
    {
        //
        // GET: /Group/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GroupReview()
        {
            ViewBag.Message = "Group Review";
            ViewBag.Domains = PowerShellExecutorLst(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Get-ADDomain.ps1");
            ViewBag.Groups = PowerShellExecutorLst(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Get-ADStewardGroups.ps1");
            return View();
        }

        [HttpPost]
        public ActionResult FillDetails(string GroupName)
        {
            ViewBag.SDC = PowerShellExecutorStr(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Get-SDC.ps1 '"+ GroupName +"'");
            return View();
        }

        // Fetching DropDownItems from Powershell Script Output
        public List<SelectListItem> PowerShellExecutorLst(string scriptPath)
        {
            string outString = "";
            var shell = PowerShell.Create();
            shell.Commands.AddCommand(scriptPath);
            var results = shell.Invoke();
            if (results.Count > 0)
            {
                var builder = new StringBuilder();
                foreach (var psObj in results)
                {
                    builder.Append(psObj.BaseObject.ToString() + "\r\n");
                }
                outString = Server.HtmlEncode(builder.ToString());
            }
            List<string> result = outString.Split(new char[] { '\n' }).ToList();
            List<SelectListItem> listItems = result.Select(s => new SelectListItem { Value = s, Text=s }).ToList();
            shell.Dispose();
            return listItems;
        }
        //Fetching String Values from Powershell Script Output
        private string PowerShellExecutorStr(string script)
        {
            string outString = "";
            var shell = PowerShell.Create();
            shell.Commands.AddCommand(script);
            var results = shell.Invoke();
            if (results.Count > 0)
            {
                var builder = new StringBuilder();
                foreach (var psObj in results)
                {
                    builder.Append(psObj.BaseObject.ToString() + "\r\n");
                }
                outString = Server.HtmlEncode(builder.ToString());
            }
            shell.Dispose();
            return outString;
        }
    }
}

查看代码:

<h2>@ViewBag.Message</h2>
<div>
    <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" })
        </div>
</div>

<div class="col-md-2">
    <div class="focus-link theme-bg-medium-blue reopen-shim">
        <a class="arrow-link theme-bg-color" id="btnSearch" data-form-method="get" type="submit" style="width:auto;" 
           tabindex="14" onclick="">search</a>
    </div>
</div>

<div class="col-md-2">
    <div class="focus-link theme-bg-medium-blue reopen-shim">
        <a id="btnReset" class="arrow-link theme-bg-color" data-form-method="get" type="submit" style="width:auto;" tabindex="15">reset</a>
        </div>
    </div>
</div>

@* Group Details to be filled*@
<div class="row">
    <div class="col-md-4">
        <div class="row">
            <div class="col-md-12 lnkLabel">
                <a href="javascript:void(0);" id="lnkDirectory" class="modal-window-sm lnkLabel" data-toggle="modal" data-target="#ModalAppList" tabindex="64" data-targeturl="@Url.Action("ShowModal", "Help", new HelpContentListViewModel() { Tag = "Directory" })">Stewardship Information</a>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                @Html.TextArea("txtSDC",null, new { @class = "form-control", @rows = "7", @disabled = "disabled" })
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="row">
            <div class="col-md-12 lnkLabel">
                <a href="javascript:void(0);" id="lnkDirectory" class="modal-window-sm lnkLabel" data-toggle="modal" data-target="#ModalAppList" tabindex="64" data-targeturl="@Url.Action("ShowModal", "Help", new HelpContentListViewModel() { Tag = "Directory" })">Business Purpose</a>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                @Html.TextArea("txtBusinessPurpose", "This is the text Area Created for the Business Purpose of the group.", new { @class = "form-control", @rows = "6", @disabled = "disabled" })
            </div>
        </div>
    </div>
</div>


@* Result Grid with the list of members *@
<div>
     Code to fill data in HTML Table or WebGrid
</div>

在脚本中,我仅使用正常命令,例如get-ADUser和get-ADGroup,我需要将下拉文本作为参数传递给FillDetails()控制器函数,但是当如何在文本区域中加载详细信息时点击事件发生了吗?

我正在调用PowerShellExecutorStr函数以在FillDetails()函数中获取值,我只想尽可能使用Viewbag将这些值加载到专用区域中。

1 个答案:

答案 0 :(得分:0)

调用此脚本的Button中的onClick事件

@section scripts{
<script>
        function GetGroupDetails()
        {
            $.ajax({
                type: "POST",
                url: "/Group/FillDetails",
                data: { GroupName: $("#ddlGroup option:selected").text().trim()} ,
                success: function (response) {        
                        $("#txtSDC").val(response.message);                   
                }
             });

        }

</script>
}

但是,控制器方法需要返回Json结果

public JsonResult FillDetails(string GroupName)
        {
            var SDC = PowerShellExecutorStr(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Get-SDC.ps1 '" + GroupName + "'");
            return Json(new { message = SDC }, JsonRequestBehavior.AllowGet);
        }