将输出c#从Web表单更改为MVC 4

时间:2014-03-31 18:30:36

标签: c# asp.net-mvc asp.net-mvc-4

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

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

        s.cmdInput = input.Text;

        output.Text = s.SSHConnect();
    }

它所做的只是返回输出SSH数据的类。我尝试过使用ViewbagViewdata,但我显然没有做对,因为它不会输出任何内容。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你可以使用我的样本:

 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>
}