MVC View与form post绑定并在同一页面显示数据

发布于 2024-09-24 01:47:37 字数 253 浏览 3 评论 0原文

当我开始开发 MVC 应用程序时,我有一个相同的查询。

我在页面中有一个文本框(用于员工姓名,因为我想以此为基础检索数据),我想发布表单并获取“员工”数据的结果相同的形式。(我想在文本框下方以相同的形式显示网格类型布局

任何人都可以知道吗如何在同一页面发布表单并显示数据

提前致谢。

As I have started working on MVC application, I have one query for the same.

I have one textbox (for employee name as I want to retrieve data basis on this) in the page , I want to post the form and get result of 'Employees' data in the same form. (I want to show grid type layout in the same form just below to the textbox)

Could anybody have any idea about how to post form and display data in the same page?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2024-10-01 01:47:37

您需要一个带有两个操作的 EmployeeControllerIndexAddEmployee

Index 操作应该检索您的员工列表并将它们传递给视图。您可以使用 ViewData 或将视图设为强类型,并在返回值中包含模型对象:return View(employeeList)

AddEmployee 操作应包含添加新员工的逻辑记录,然后使用 RedirectToAction 重定向回 Index 操作。这称为后重定向获取模式。您永远不应该让您的用户“登陆”POST 响应,因为这可能会导致双重表单提交。

在员工视图中:您需要上面一个表单和下面一个网格(例如 MVCContrib Grid)。表单的操作应该是EmployeeControllerAddEmployee 操作。

You need a EmployeeController with two Actions: Index and AddEmployee.

The Index action should retrieve your list of Employees and hand them to the View. You can use ViewData or make the View strongly typed and include the model object in the return: return View(employeeList)

The AddEmployee action should contain the logic to add the new employee record, then redirect back to the Index action using RedirectToAction. This is known as the Post-Redirect-Get pattern. You should never have your users "land" on a POST response, as it can result in double form submissions.

In the Employee View: You need a form above and a grid (e.g. MVCContrib Grid) below. The Action of the form should be the AddEmployee action of the EmployeeController.

水染的天色ゝ 2024-10-01 01:47:37

我知道这不是您想要的答案,但在我看来您正在尝试模仿 WebForms 的回发行为。

不要这样做

您不应该从后期操作中渲染视图。如果用户尝试刷新结果页面,则 post 操作将再次执行。这可能会导致重复数据(例如在数据库 Web 前端应用程序中)。

您应该使用 PRG 模式。请阅读 或 < a href="http://devlicio.us/blogs/tim_barcz/archive/2008/08/22/prg-pattern-in-the-asp-net-mvc-framework.aspx" rel="nofollow noreferrer">这个。

I know this is not the answer you're asking for, but it seems to me you are trying to mimic the post-back behavior of WebForms.

Don't do that.

You should NOT render views from post actions. If the user tries to refresh the result page, the post action will be executed again. This can result in duplicated data (in a database web front-end application, for instance).

You should use the PRG pattern. Please read this, this or this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文