Friday, 20 March 2020

Pass View TextBox Value In Controller in Asp.Net MVC

3/20/2020 - By Pranav Singh 0


This article will show you how you can pass, send or access  view textbox value in controller in asp.net mvc. In this article i will explain how you can pass TextBox, TextBoxFor and Input textbox value from view to controller end on submit button click.

So for this first we will create a new asp.net mvc application, and add a controller class.  In this we will take an actionresult method and create a view.

  [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

View of above method

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

TextBox: For this add a @Html.TextBox razor control.

@{
    ViewBag.Title = "Index";

}
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.TextBox("StudentName")
    <input type="submit" value="Submit" />
}

Now go to controller end and in post method add the below bode as show below.

        [HttpPost]
        public ActionResult Index(string StudentName)
        {
            return View();
        }

Please check the highlighted part of the above code. In this post method i have passed a parameter named as StudentName Which is exactly same as the name of the control. If you make the control name different you will not be able to access the control value. Now run the application and check output by outing the break point.
Now check the controller end  you will get the value.


TextBoxFor: First we will create a model class as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace StateManagement.Models
{
    public class StudentModel
    {
        public string StudentName { get; set; }
    }
}

For this add a @Html.TextBoxFor razor control with model.

@model StateManagement.Models.StudentModel
@{
    ViewBag.Title = "Index";

}
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.StudentName)
    <input type="submit" value="Submit" />
}

Now at controller end we will pass the model class name as parameter in post method.

[HttpPost]
        public ActionResult Index(string StudentName)
        {
            return View();
        }

Now put the break point and run the application.


AS you click on submit button you will get the break point will hit and here is you output.



HTML Input: In this first you need to add an input control as shown below.

@{
    ViewBag.Title = "Index";

}
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    <input type="text" name="StudentName" />
    <input type="submit" value="Submit" />
}

Now at controller end put the below code.

        [HttpPost]
        public ActionResult Index(string StudentName)
        {
            return View();
        }
Here you will see that the parameter name is same as the text input control. Now run the application and click on submit.



As you click on submit button you break point will hit and you below get your value in parameter.





DOWNLOAD

Thanks for reading this article. Please let me know you view and comment.





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