Monday, 17 January 2022

Asp.net Core 6: Access/Read/Pass TextBox and TextboxFor Value From View To Controller In C#.Net

1/17/2022 - By Pranav Singh 0

 In this article i will show you how you can access or get the textbox or textbox for value in asp.net core 6 / mvc application at controller end using c#.net.  So in this article first we will check how we can access the TetBox control value at controller end.


TextBox:

At controller end first we will index action method, and create a view. In this view we will add the below code.


@{

    ViewData["Title"] = "Home Page";

}

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "formxmldata", @enctype = "multipart/form-data" }))

{

    <b>Get TextBox Value </b>

    <span style="color:red">@ViewBag.Data</span>

    <br />

    @Html.TextBox("empname", "", new { @class = "form-control", @placeholder = "Please enter employee name" })

    <br />

    <input type="submit" value="Submit" />

}

    

Now we will add the below code at controller end.

[HttpGet]

        public IActionResult Index()

        {

            return View();

        }

        [HttpPost]

        public IActionResult Index(string empname)

        {

            ViewBag.Data = empname;

            return View();

        }


In above code just check the parameter name passed in HttpPost method. The parameter name is same as the TextBox control name

Now lets run the code and check the output.
Asp.net Core 6: Access/Read/Pass TextBox and TextboxFor Value From View To Controller In  C#.Net

Now check the controller end detail. 

Asp.net Core 6: Access/Read/Pass TextBox and TextboxFor Value From View To Controller In  C#.Net

Here we are getting entered value at controller end. 

Asp.net Core 6: Access/Read/Pass TextBox and TextboxFor Value From View To Controller In  C#.Net

TextBoFor:

For getting data from TextBoxFor control we will first create a model class:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks; 

namespace Project.Models

{

    public class EmpNameModel

    {

        public string EmpName { get; set; }

    }

}


Now we will create the view and add the below code into it.

@model Project.Models.EmpNameModel

@{

    ViewData["Title"] = "Home Page";

}

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "formxmldata", @enctype = "multipart/form-data" }))

{

    <b>Get TextBox Value </b>

    <br />

    <span style="color:red">@ViewBag.Data</span>

    <br />

    @Html.TextBoxFor(m => m.EmpName, "", new { @class = "form-control", @placeholder = "Please enter employee name" })

    <br />

    <input type="submit" value="Submit" />

}


In above code i have bind the model property to the the control. Now add the below code on controller end.

  [HttpGet]

        public IActionResult Index()

        {

            return View();

        }

        [HttpPost]

        public IActionResult Index(EmpNameModel empNameModel)

        {

            ViewBag.Data = empNameModel.EmpName;

            return View();

        }


Now we have done run the code and check the output.

Asp.net Core 6: Access/Read/Pass TextBox and TextboxFor Value From View To Controller In  C#.Net

Now click on submit button and check the detail at controller end.

Asp.net Core 6: Access/Read/Pass TextBox and TextboxFor Value From View To Controller In  C#.Net

Now here is the final output.

Asp.net Core 6: Access/Read/Pass TextBox and TextboxFor Value From View To Controller In  C#.Net


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top