Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将输出c#从web表单更改为MVC4_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

将输出c#从web表单更改为MVC4

将输出c#从web表单更改为MVC4,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我是MVC新手,但了解其背后的概念和逻辑,我创建了一个web表单版本的网站,但想将其更改为MVC。我有一个问题,我很难找到解决方案。我在web表单中的代码是: protected void CMD(object sender, EventArgs e) { SSH s = new SSH(); s.cmdInput = input.Text; output.Text = s.SSHConnect(); } 它所做的只是获取输出

我是MVC新手,但了解其背后的概念和逻辑,我创建了一个web表单版本的网站,但想将其更改为MVC。我有一个问题,我很难找到解决方案。我在web表单中的代码是:

protected void CMD(object sender, EventArgs e)
    {
        SSH s = new SSH();

        s.cmdInput = input.Text;

        output.Text = s.SSHConnect();
    }

它所做的只是获取输出SSH数据的类的返回。我尝试过使用
Viewbag
Viewdata
,但显然做得不对,因为它不会输出任何东西。有办法做到这一点吗?

如果我正确理解了您的问题,您可以使用我的示例:

 public class CmdController : Controller
 {

    public CmdController()
    {         
    }

    [HttpGet]
    public ActionResult Cmd()
    {
        return View((object)"Greetings!");
    }

    [HttpPost]
    public ActionResult Cmd(string inputCommand)
    {
        SSH s = new SSH();

        s.cmdInput = inputCommand;

        string outputText = s.SSHConnect();

        return View((object)outputText);
    }
 }


// Simple partial view for Cmd Action
// You can use your own class instead of string model.
@model string

@using (Html.BeginForm())
{
    <strong>command output:</strong>
    <div>@Model</div>
    @Html.TextBox("inputCommand")
    <button type="submit">Go!</button>
}
公共类CmdController:Controller
{
公共CmdController()
{         
}
[HttpGet]
公共行动结果Cmd()
{
返回视图((对象)“问候语!”);
}
[HttpPost]
公共操作结果Cmd(字符串输入命令)
{
SSH s=新的SSH();
s、 cmdInput=inputCommand;
字符串outputText=s.SSHConnect();
返回视图((对象)输出文本);
}
}
//Cmd操作的简单局部视图
//您可以使用自己的类而不是字符串模型。
@模型串
@使用(Html.BeginForm())
{
命令输出:
@模型
@Html.TextBox(“inputCommand”)
走!
}

输出是否为ASP.NET控件,例如?
“我已尝试使用Viewbag和Viewdata”
-能否显示您的尝试?你可能已经很接近了。这些机制通常用于将数据元素传递给视图(不过最好使用模型)。有没有一种不使用表单的方法来实现这一点?因为我想用它作为一种类型,在不刷新页面的情况下按enter键(类似于putty终端)。如果您不喜欢表单,可以使用ajax请求对您的操作执行,并将outputText作为Json对象返回。替换为:返回视图((对象)outputText);对此:返回Json(new{Text=outputText});您是否有可能使用代码向我展示这一点,因为我从未使用过Ajax和JSON。在此,您可以看到在ASP.NETMVC中使用ajax的简单示例。