【问题标题】:How do I fill the text area using controller method on button click in MVC view?如何在 MVC 视图中单击按钮时使用控制器方法填充文本区域?
【发布时间】:2019-06-03 15:35:41
【问题描述】:

我创建了 PowerShell 脚本来获取活动目录组,当网页加载组名时,组名会填入下拉列表。我要做的下一件事是使用搜索按钮单击事件,通过传递 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 将值加载到专用区域中。

【问题讨论】:

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


    【解决方案1】:

    在Button in的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);
            }
    

    【讨论】:

      猜你喜欢
      • 2012-02-16
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多