How to get html.textbox input value in ASP.NET MVC 2
I currently have TextBoxusing:
<%: Html.TextBox("TextBox1") %>
How to get the value of what is entered as TextBoxa string so that I can use this string variable throughout the application?
In the view, it monitors inheritance at the top of the page for the model. This page is called "InputNumbersSection":
<%: Html.TextBoxFor(m => m.Number) %>
and action:
<%: Html.ActionLink("Get Number!", "DisplayNumbersSection") %>
The model has the following:
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();
}
ViewData, which I use on another page to return the number from the text box entered in the view.
When I enter somthing in a text box, I don’t see the property hit or execute. The "Number" property returns NULL all the time. It doesn't seem like what I am typing in a TextBox.
, :
public class YourModel
{
public string YourProperty { get; set; }
}
, :
public ActionResult YourEvent()
{
return View(new YourModel());
}
, Page :
<%@ Page Title="" Inherits="System.Web.Mvc.ViewPage<YourModel>" %>
, , :
<%: Html.TextBoxFor(m => m.YourProperty) %>
, :
[HttpPost]
public ActionResult YourEvent(YourModel model)
{
if (ModelState.IsValid)
{
string TextBoxValue = model.YourProperty;
// do what you want with it
}
// do something
}
- , , Request Form.
: .
. "InputNumbersSection":
<%: Html.TextBoxFor(m => m.Number) %>
:
<%: Html.ActionLink("Get Number!", "DisplayNumbersSection") %>
:
public class NumberModels
{
public string Number { get; set; }
}
:
public ActionResult DisplayNumbersSection(NumberModels model)
{
if (ModelState.IsValid)
{
string TextBoxValue = model.Number;
ViewData["Number"] = TextBoxValue;
}
return View();
}
ViewData, , , .
When I enter somthing in a text box, I don’t see the property hit or execute. The "Number" property returns NULL all the time. It doesn't seem like what I am typing in a TextBox.