简体   繁体   English

如何调用ac#函数将值填入razor html文本框?

[英]How to invoke a c# function to fill value into razor html textbox?

I am using the function that fetches the output from PowerShell script into the output String variable. 我正在使用从PowerShell脚本获取输出到输出String变量的函数。 I need to call that function inside an @HTMLTextBox to load the value. 我需要在@HTMLTextBox中调用该函数来加载值。

I have created the function and using razor format to invoke the function inside the textbox element. 我已经创建了该函数并使用razor格式来调用textbox元素中的函数。

@using System.Management.Automation
@functions {

public string PowerShellExecutor(string script) {
    var shell = PowerShell.Create();
    string outString = "";
    shell.Commands.AddScript(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());
    }
    return outString;
  }
}

<div class="col-md-12">
                @Html.TextBox("txtDirectory", @PowerShellExecutor("$env:USERDNSDOMAIN") , new { @class = "form-control", @type = "number"})
</div>

The textbox is suppose to fill the value of the output obtained when the command is passed through the function. 文本框用于填充命令通过函数时获得的输出值。 However, It shows blank. 但是,它显示为空白。

删除PowerShellExecutor的@,您还应删除@type = "number"以使文本框接受字符串。

@Html.TextBox("txtDirectory", PowerShellExecutor("$env:USERDNSDOMAIN") , new { @class = "form-control"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM