Introduction
This article shows you how to use a DisplayTextfor helper to handle HttpPost operations in MVC applications.
Create an ASP.Net Web Application as in Figure 1.
Choose MVC template as in Figure 2.
Setup Entity Framework as shown in Figures 3, 4 and 5.
Add an Employee Controller as shown in Figures 6, 7 and 8.
EmployeeController.cs
- using DisplayTextForMVCPost_App.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- namespace DisplayTextForMVCPost_App.Controllers
- {
- public class EmployeeController : Controller
- {
- EmployeeEntities db = new EmployeeEntities();
- //
- // GET: /Employee/
- public ActionResult Index()
- {
- return View(db.Departments.ToList());
- }
- [HttpPost]
- public string Index(IEnumerable<Department> dpt)
- {
- StringBuilder sb = new StringBuilder();
- foreach (var item in dpt)
- {
- sb.Append("The list of departments: <b>" + item.DepartmentName + "</b>");
- sb.Append("<br />");
- }
- return sb.ToString();
- }
- }
- }
Add a View as Figure 9.
Index.cshtml
- @model IList<DisplayTextForMVCPost_App.Models.Department>
- @{
- Layout = null;
- }
- @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
- {
- for (int i = 0; i < Model.Count; i++)
- {
- @Html.HiddenFor(m => m[i].DepartmentName)
- @Html.DisplayTextFor(m => m[i].DepartmentName)
- <br />
- }
- <br />
- <input type="submit" value="Submit" />
- }
The output of the application is as in Figure 10.
Summary
In this article we saw how to use a DisplayTextFor Helper to handle HttpPost operations in MVC application.
Happy coding!

SharadPosted Jul 22, 2015, 12:35 AM
nice work
Santhakumar MunuswamyPosted Jun 6, 2015, 2:36 AM
Thanks for nice article