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>
ViewBag
. It's untyped and therefore unsafe. It should only be used for prototyping, imo.POST
form request.
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.
MaxLength
vs StringLength
).<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.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
:
nameof()
to ensure you have the correct Action Name.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>
}