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.
Some of my previous articles are as follows: Display
DropdownList Item By Group in Asp.Net MVC Using C#.Net, Insert,
Update, Delete Operation In Asp.Net MVC Using Entity Framework C#.Net ,CRUD
Functionality, File
Upload and Displaying Them as Thumbnails in DataList in MVC3 By Using Razor,
C#.Net, Validation
for Accepting Terms and Conditions Checkbox in Asp.Net MVC Using jQuery, jQuery
UI Calender Open With LightBox Effect in Asp.Net MVC, Frozen
Rows and Columns in Asp.Net Mvc Webgrid Using jQuery Like Excel Sheet, How
to Set Asp.net MVC Controller Post Method Parameter Default Value Using C#.Net.
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
Thank you
ReplyDeleteThank you
ReplyDeleteHi Shrusv,
DeleteThanks for your valuable comment. keep visiting.
Hlo,
Deletei need one help.how to pass a texbox value to one view to two controller action.
Thank you :)
ReplyDeleteThanks for your valuable comment
Deletei have a question plz answer me...
ReplyDeleteusing Student_UsingMVC.Models;
ReplyDeleteusing 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
very nice, it is helpful
ReplyDeleteThanks for your valuable commnet.
Delete