How to get the value o...">

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.

+3
5

Request.Form

.

.

+1

, :

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.

: .

+8

- TextBox1, Action , , TextBox1 , . .. , :

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

Function Home(ByVal FirstName As String) As ActionResult
  MsgBox(FirstName)
End Function
+1

, vb, #.

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

. "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.

0
source

Source: https://habr.com/ru/post/1754168/


All Articles