Search code examples
c#asp.netweb-applicationstextbox

Facing Problems in Getting the values of textboxes in C# Web Application with details below:


I am facing issue in getting the values from Post Method from one view to another. I have tried everything but still I cannot get the values.

Here is My Code:

EmpController:

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

namespace My_Work.Controllers
{
    public class EmpController : Controller
    {
        // GET: Emp
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult Emp()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Emp(string name,string design)
        {
            ViewBag.Name1 =name;
            ViewBag.Design1 = design;
            return View();
        }
        public ActionResult EmpDtl()
        {
            return View();
        }
    }
}

Emp View:

   @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Employee Details</title>
        <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    </head>
    <body>
        @using (Html.BeginForm("EmpDtl","Emp", FormMethod.Post))
        {
            <h2>Employee Details</h2>
            <hr />
                <table cellpadding="2" cellspacing="0" border="0">
                    <tr>
                        <td>Employee Name:</td>
                        <td>
                            @Html.TextBox("Name")
                        </td>
                    </tr>
                    <tr>
                        <td>Designation:</td>
                        <td>
                            @Html.TextBox("Design")
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input type="submit" value="Submit" /></td>
                    </tr>
                </table>
        }
    </body>
    </html>

EmpDtl View:

@{
    Layout = null;
 }
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Employee Details</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <h2>Employee Details</h2>
    <br />
    <br />
    <span>Employee Name: </span>@ViewBag.Name1
    <br />
    <span>Designation: </span>@ViewBag.Design1
</body>
</html>

This is the code where I cannot get the values of textboxes on Post Method and I have tried everything but cannot find the issue. Any help will be appreciated to solve this issue.

@{
    Layout = null;
 }
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Employee Details</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <h2>Employee Details</h2>
    <br />
    <br />
    <span>Employee Name: </span>@ViewBag.Name1
    <br />
    <span>Designation: </span>@ViewBag.Design1
</body>
</html>

Solution

    • Don't use the ViewBag. It's untyped and therefore unsafe. It should only be used for prototyping, imo.
    • You need a ViewModel class (separate classes for each view or logical page) which represents both the content of the rendered page and the data that you want to bind from an incoming POST form request.
      • DO NOT use Entity Framework types as bound ViewModels: while it may seem convenient (and even some of Microsoft's older tutorials demonstrate this anti-technique) it introduces major security issues because it allows users to arbitrary specify values for any value (such as UserId and Password). Also it just doesn't work in practice because the contents of a web-page do not map 1:1 with a single database row or domain-entity: consider an "Edit user" page: it will have two password inputs ("New Password" and "Confirm password") those properties won't exist on your User entity class.
        • Also ASP.NET MVC uses different data-annotations to Entity Framework and they can conflict (e.g. MaxLength vs StringLength).
      • I note that an awkward limitation of ASP.NET MVC's design means you cannot have have multiple <form> elements on the same page if you use HtmlHelper to generate form inputs. There are ways around this but I won't go into detail as that's off-topic for this conversation.
      • Another annoying limitation is that if you use controller action method parameter binding (instead of using UpdateModel explicitly) you cannot have immutable viewmodel-types: they must have a default (parameterless) constructor, even if that puts the viewmodel object into an invalid state.

    Anyway, this is what your View-Model would look like:

    class EmployeeDetailsViewModel
    {
        [Display( Name = "Employee Name" )]
        [Required]
        public string Name   { get; set; }
    
        [Required]
        public string Designation { get; set; }
    }
    

    Change your POST action's signature:

    [HttpPost]
    public ActionResult Emp( EmployeeDetailsViewModel model ) // IIRC the parameter must be named "model" in ASP.NET MVC, I might be wrong. It's been a while.
    {
        
    }
    

    And change your .cshtml:

    • Tip: Use nameof() to ensure you have the correct Action Name.
    • Unfortunately you cannot use nameof() for the Controller Name because the Controller class name suffix won't be bound correctly. (Yet another minor annoyance in ASP.NET MVC)
    @model EmployeeDetailsViewModel
    
    ...
    
            @using (Html.BeginForm( action: nameof(EmpController.EmpDtl), controller: "Emp", FormMethod.Post))
            {
                <h2>Employee Details</h2>
                <hr />
                    <table cellpadding="2" cellspacing="0" border="0">
                        <tr>
                            <td>Employee Name:</td>
                            <td>
                                @Html.TextBoxFor( m => m.Name )
                            </td>
                        </tr>
                        <tr>
                            <td>Designation:</td>
                            <td>
                                @Html.TextBoxFor( m => m.Designation )
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type="submit" value="Submit" /></td>
                        </tr>
                    </table>
            }