Asp.Net Core 8 MVC: How to Get TextBox Value Using C# In Controller

How to get or retrieve TextBox and TextBoxFor value using C# In controller. display value in view with viewbag in controller C#.net, asp.net core mvc
In today's article I will show you how you can bind, display or capture the selected value of TextBox and TextBoxFor added value at the controller end in your asp.net core 8 mvc application using c#.net. In my previous article I had shown you "Asp Net Core 8: Calendar Control Without Using jQuery In MVC, ". For this article we will first create a new asp.net core 8 mvc application and add a controller. 

So, we will first check how we can access the value of TextBoxFor control in asp.net core 8 mvc application using c#.net. 

How to Access Value of TextBoxFor Control in Asp.net Core 8 MVC In C#

First, we will add a model class file for c#.net. This file name we keep as "UserModel". This will be our model file. In this file we will keep two properties as shown below.
namespace Project.Models
{
    public class UserModel
    {
        public string Name { get; set; }
        public string Location { get; set; }
    }
}
What is the Model Class in Asp.Net Core MVC Application? A model class file in asp.net core mvc application represents the business logic. Model class file we add in Model folder. In a lay man language the Model class file contain properties which represents the fields structure of controls available on the screen.
First, we will add HttpGet and HttpPost methods in the added controller. Please check the below code.
using Microsoft.AspNetCore.Mvc;
using Project.Models;
using System.Diagnostics;

namespace Project.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Index(UserModel userModel)
        {
            return View();
        }
    }
}
In above code method having HttpGet attribute will act as a page load method. On other hand the method having HttpPost will execute when we submit the form. In HttpPost method I have passed the model class name as parameter. In this model class property, we will get the added value of the controls. Now, we add a view and add the code below.
@model UserModel
@{
    ViewData["Title"] = "Home Page";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @Id = "formdata", @enctype = "multipart/form-data" }))
{
    <div class="text-center">
        <b>Name:</b>
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        <br />
        <b>Location:</b>
        @Html.TextBoxFor(m => m.Location, new { @class = "form-control" })
        <br />
        <input type="submit" value="Submit" />
        <br />   
        <br />
        <b>Your Name: </b>@ViewBag.Name  <br />
        <b>Your Location: </b>@ViewBag.Location
    </div>
}
In code above, first I have added the reference of the model class. Below that, the form tag has been added with, in this form method type and, Id of the form, controller name and the action name have defined. So when the user clicks on the submit button the specific action method will execute at the controller end. 

As in the code above I have shown that the controller is having an Index method decorated with HttpPost. 

In the code I have used the model class property to bind the TexttBoxFor control. Binding the ViewModel property will help us to get the added value in control at the controller end.

In the following I have used ViewBag to to store and display the captured value at the controller end. Now we will make some modifications in the our httppost method code assig the user added value and pass it to view.
 [HttpPost]
 public IActionResult Index(UserModel userModel)
 {
     ViewBag.Name = userModel.Name;
     ViewBag.Location = userModel.Location;
     return View();
 }
In the code above we will get the user added value in the model class property and assign it to ViewBag to display it on view. Now we will run the code to check the output.
Asp.Net Core 8 MVC: How to Get TextBox Value Using C# In Controller

Now we add value and click on the submit button.

How to Get TextBoxFor Value Using C# In Controller

The controller will get the added value n respective model class property.


TextBoxFor value at controller in C#.net

Now press F5 and check the output.

TextBoxFor value at controller in C#.net and display in view
What is the difference Between TextBox and TextBoxFor in Asp.Net Core MVC?
  • In Asp.Net Core MVC or Asp.Net MVC TextBoxFor is a strongly type and on other hand TextBox is a not a strongly type control. At the end of the execution both will generate the Input tag of control type text.
  • Here in case of TextBoxFor control the name of control will be the ModelName_Property (Eg: UserModel_Name). On other hand the name of control will be the same name which we provided.
  • Here the name of the controls will help up to get the control value at the controller end. We need to pass the name or the model class as parameter in the HttpPost method.

How to Access Value of TextBox Control in Asp.net Core 8 MVC In C#?

To display the textbox control using TextBox in asp.net core mvc application in controller we will add the HttpGet and HttpPost method.
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Index(string Name, string Location)
        {          
            return View();
        }
In above code I have added Index method decorated with HttpGet and HttpPost method. As above i already excplined action both attribute. Here in method having HttpPost, I have passed two parameters of string named as "Name" and "Location". This parameter will help us to get the user added value at view end.

Now we will create the view and the below code into it.
@{
    ViewData["Title"] = "Home Page";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @Id = "formdata", @enctype = "multipart/form-data" }))
{
    <div class="text-center">
        <b>Name:</b>
        @Html.TextBox("Name", "", new { @class = "form-control" })
        <br />
        <b>Location:</b>
        @Html.TextBox("Location", "", new { @class = "form-control" })
        <br />
        <input type="submit" value="Submit" />
        <br />
        <br />
        <b>Your Name: </b>@ViewBag.Name  <br />
        <b>Your Location: </b>@ViewBag.Location
    </div>
}
In above code I have used TextBox control. In TextBox control I have passed name, value and the attribute of for the control. Here one thing we need to take care that the control name and the passed parameter name at the controller end. 

Here I have used ViewBag to display the user entered value.  Now we will make some modification in our controller code to assign the entered value to ViewBag.
        public IActionResult Index(string Name, string Location)
        {
            ViewBag.Name = Name;
            ViewBag.Location = Location;
            return View();
        }
In above code I have assign the method parameter value to ViewBag to display it at View end.  Now we will run the code and check the output.

How to Get TextBox Value Using C# In Controller

Now we will add detail and click on submit button. Put a break point to check how the code will work at controller end.

How to Get TextBox Value Using C# In Controller

Ones the break point hit. Check the passed parameter. We are getting the added value.

How to Get TextBox Value Using C# In Controller

Now press F5 to check the final output.

How to Get TextBox Value Using C# In Controller

Post a Comment