0

I have implemented a simple form in MVC with Razor syntax.

@model QuisenberryMVC001.Models.ConsoleCommand    

@using (Html.BeginForm())
{
    <fieldset>
        <legend>Product</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.input)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.input)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.output)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.output)
        </div>

        <input type="submit" value="Run" />
    </fieldset>
}

The model:

public class ConsoleCommand
    {
        public String input {get; set;}
        public String output { get; set;}
    }

This form displays a text box to accept input and a text area to display output. I have implemented a controller to prepare the output. Ultimately, this will be done by the model.

Here is the controller:

[HttpPost]
public ActionResult MyConsole(ConsoleCommand command)
{
    ViewBag.Message = "My Console";
    command.output += "My Output";
    return View(command);
}

I would like for clicking the "Run" button to update the text area with the text "My Output". Instead, it displays whatever value the user typed.

When I debug the application, I see that command.output actually is "My Output" when return View(command) is reached. I am unable to see what value the view receives, as putting a watch on model or model.output results in an error like The name 'model' does not exist in the current context.

It looks the values are ready correctly from the form but not written correctly to the form. How do I fix binding?

Jacob Quisenberry
  • 1,131
  • 3
  • 20
  • 48

0 Answers0