0

I'm new to MVC but understand the concept and logic behind it, I created a version of my website in web forms but wanted to change it to MVC. I am having one issue which I'm having trouble finding a solution. My code in web forms is:

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

        s.cmdInput = input.Text;

        output.Text = s.SSHConnect();
    }

All it is doing is taking the return of a class that outputs SSH data. I've tried using Viewbag and Viewdata but I obviously wasn't doing it right because it wouldn't output anything. Is there a way to do this?

Tommy
  • 39,592
  • 10
  • 90
  • 121
ValMangul
  • 49
  • 5
  • 16
  • Is output an ASP.NET control, e.g. ? – MatthewMartin Mar 31 '14 at 18:37
  • 2
    `"I've tried using Viewbag and Viewdata"` - Can you show your attempt? You might have been close. Those mechanisms are generally used for passing data element to a view (though using a model is preferred). – David Mar 31 '14 at 18:38

1 Answers1

0

If I understood your question correctly, you can use my sample:

 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>
}
SKorolchuk
  • 306
  • 1
  • 3
  • 8
  • Is there a way of doing this without using a form? As I would like to use it as a sort of type and hit enter(Similar to a putty terminal) without a page refresh. – ValMangul Mar 31 '14 at 19:07
  • You can use ajax request to your action and return outputText as Json object, If you don't like forms. Replace this: return View((object)outputText); To this: return Json(new { Text = outputText }); – SKorolchuk Mar 31 '14 at 19:10
  • Is there any chance you could show me this using code, as I've never used Ajax and JSON. Thank you in advance. – ValMangul Apr 15 '14 at 17:54
  • You can see simple examples of ajax using in ASP.NET MVC there. [link](http://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor) – SKorolchuk Apr 16 '14 at 16:54