views:

58

answers:

5

Hello all,

I currently have a TextBox using:

<%: Html.TextBox("TextBox1") %>

How do I get the value of what is typed into the TextBox as a string so that I can use that string variable throughout my application?

The view has he following with the inherits on top of page to model. This page is named "InputNumbersSection":

<%: Html.TextBoxFor(m => m.Number) %>

and the action:

<%: Html.ActionLink("Get Number!", "DisplayNumbersSection") %>

The Model has this:

 public class NumberModels
    {
        public string Number { get; set; }
    }

The controller has the following:

public ActionResult DisplayNumbersSection(NumberModels model)
        {

            if (ModelState.IsValid)
            {
                string TextBoxValue = model.Number;
                ViewData["Number"] = TextBoxValue;
            }      
            return View();
        }

The ViewData I use in another page to return the number from the textbox typed in the view.

When I type somthing into the textbox, I do not see the property getting hit or executed. The "Number" property returns NULL all the time. It almost seems as if it is not picking up what I type into the TextBox

A: 

You can get it from the

Request.Form

But this is very primitive solution.

You must use strongly typed views and work with your model in actions.

griZZZly8
+1  A: 

If you are requiring the TextBox1 variable server side, the textbox must be inside a form posting to an Action in a controller, this will mean the TextBox1 variable will be available as a parameter of the action that the form posts to. ie In your view you could have:

<% Using Html.BeginForm() %>
   <%: Html.TextBox("FirstName") %>
   <Input type=submit />
<% End Using %>

Then in the controller you would have the method

Function Home(ByVal FirstName As String) As ActionResult
  MsgBox(FirstName)
End Function
CodeKiwi
+5  A: 

Well to start you should have a model for your view:

public class YourModel
{
    public string YourProperty { get; set; }
}

Your controller that will create the initial view:

public ActionResult YourEvent()
{
    return View(new YourModel());
}

To create a strongly typed view you need to add the following to the Page directive:

<%@ Page Title="" Inherits="System.Web.Mvc.ViewPage<YourModel>" %>

Then in your view you can do:

<%: Html.TextBoxFor(m => m.YourProperty) %>

Then in your controller when you post the form:

[HttpPost]
public ActionResult YourEvent(YourModel model)
{
    if (ModelState.IsValid)
    {
         string TextBoxValue = model.YourProperty;
         // do what you want with it
    }            
    // do something
}

A stongly typed view is the best way to go but if you want to do it quick and dirty you can always access the Request object and check the Form.

Edit: Added the strongly typed view definition.

Kelsey
Remember to add Inherits="System.Web.Mvc.ViewPage<YourModel> to the page header. Otherwise this is the best solution.
Xenph Yan
@Xenph Yan thanks, as you suggested, I added the definition which makes it a more complete answer.
Kelsey
+1  A: 

Same as vb solution posted but in c#.

[HttpPost]
public ActionResult YourActionMethod(string TextBox1)
{
    //use value of TextBox1
}
David Liddle
Hahah yeah I guess VB is now the exception rather than the rule, I am currently in the process of improving my C#. :P
CodeKiwi
A: 

The view has he following with the inherits on top of page to model. This page is named "InputNumbersSection":

<%: Html.TextBoxFor(m => m.Number) %>

and the action:

<%: Html.ActionLink("Get Number!", "DisplayNumbersSection") %>

The Model has this:

 public class NumberModels
    {
        public string Number { get; set; }
    }

The controller has the following:

public ActionResult DisplayNumbersSection(NumberModels model)
        {

            if (ModelState.IsValid)
            {
                string TextBoxValue = model.Number;
                ViewData["Number"] = TextBoxValue;
            }      
            return View();
        }

The ViewData I use in another page to return the number from the textbox typed in the view.

When I type somthing into the textbox, I do not see the property getting hit or executed. The "Number" property returns NULL all the time. It almost seems as if it is not picking up what I type into the TextBox

Mage
@Gin an `ActionLink` does not post the data. All it does is make a link. You need to have your data in a `form` and use an `input` to post the data to your controller. Also as a side note, you should edit your original question and add to it. This section is for answers only. You can edit as much as you need so you should just keep adding/editting until you get the answer you require. Also don't forget to make the accepted answer when you are done.
Kelsey