Wednesday, 27 August 2014

Access TextBox and TextboxFor Value From View To Controller In Asp.net MVC, C#.Net

8/27/2014 - By Pranav Singh 10

This article will show you how you can pass the textbox and textbox for control value from view to the controller in an MVC application. In this I will show you how to get the value of textbox and the textboxfor which is bind with a model in on postback in controller. This application you can use in MVC3, MVC4 and MVC5 application.


So I will explain this article in two parts first how you can get the textbox control value at controller end.

PART 1: TextBox

Now here is the view.

@{
    ViewBag.Title = "Access TextBox and TextboxFor Value From View To Controller In Asp.net MVC";
}
@using (Html.BeginForm("Index", "Home"))
{
     <b>Acess Textbox </b>
    @Html.TextBox("Name","")
    <br />
    <input type="submit" value="Submit TextBox" />
}

Here is the controller code.

 [HttpPost]
 public ActionResult Index(int id = 0)
 {
  string name = Request.Form["Name"].ToString();
  return View();
 }

Now run the application to check the output.



Now click on button for making postback.


Here what you need

PART 2:  TextBoxFor

Now here is the view for textboxfor control. For this article first we will create model class for this. In this we will bind the control with model and on post back we will access the model control value. so her is  the model class.

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

namespace MvcApplication6.Models
{
    public class StudentModel
    {
        public string Name { get; set; }
    }
}

After this we will create view for the controller.

@model  MvcApplication6.Models.StudentModel
@{
    ViewBag.Title = "Access TextBox and TextboxFor Value From View To Controller In Asp.net MVC";
}
@using (Html.BeginForm("Index", "Home"))
{
    <b>Acess Textbox </b>
    @Html.TextBoxFor(m=>m.Name)
    <br />
    <input type="submit" value="Submit TextBoxFor" />
}

Here is the completer code for the controller.

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

namespace MvcApplication6.Models
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(StudentModel objstudentmodel)
        {
            string name = objstudentmodel.Name;
            return View();
        }
    }
}

Now run the application and check the output.


Now click on button to make postback



Here what you need


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

10 comments:

  1. Replies
    1. Hi Shrusv,
      Thanks for your valuable comment. keep visiting.

      Delete
    2. Hlo,
      i need one help.how to pass a texbox value to one view to two controller action.

      Delete
  2. i have a question plz answer me...

    ReplyDelete
  3. using Student_UsingMVC.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace Student_UsingMVC.Controllers
    {
    public class StudentController : Controller
    {
    StudentsEntities objStuEn = new StudentsEntities();
    // GET: Student
    public ActionResult Index()
    {
    return View();
    }
    [HttpPost]
    public ActionResult Index(StudentModel objStuModel)
    {
    StudentsRecord objStuRe = new StudentsRecord();
    objStuRe.studentName = objStuModel.studentName;
    objStuRe.fatherName = objStuModel.fatherName;
    objStuRe.rollNo = objStuModel.rollNo;
    objStuRe.phoneNo = objStuModel.phoneNo;
    objStuRe.city = objStuModel.city;
    return View();
    }
    }
    }

    i want to send data from ViewModel to DomainModel

    ReplyDelete

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