【发布时间】:2021-09-06 12:39:05
【问题描述】:
我在将 Post Method 的值从一个视图转移到另一个视图时遇到问题。
我已经尝试了所有方法,但仍然无法获得值。
这是我的代码:
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查看:
@{
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查看:
@{
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>
这是我无法在 Post Method 上获取文本框值的代码,我已经尝试了所有方法但找不到问题。我们将不胜感激任何帮助来解决这个问题。
@{
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>
【问题讨论】:
-
您应该使用
TextBoxFor而不是TextBox。 -
“我已经尝试了一切” - 不,你还没有“尝试一切”。如果您已经尝试了所有方法,那么您会发现使用 ViewModel 类与
HtmlHelper一起使用的解决方案,这样就可以了。
标签: c# asp.net web-applications textbox