开发者

How do I get the value of an input of html.textbox in ASP.NET MVC 2

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)
        开发者_如何学JAVA    {
                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


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.


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.


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


Same as vb solution posted but in c#.

[HttpPost]
public ActionResult YourActionMethod(string TextBox1)
{
    //use value of TextBox1
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜