MVC View与form post绑定并在同一页面显示数据
当我开始开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个带有两个操作的
EmployeeController
:Index
和AddEmployee
。Index
操作应该检索您的员工列表并将它们传递给视图。您可以使用 ViewData 或将视图设为强类型,并在返回值中包含模型对象:return View(employeeList)
AddEmployee
操作应包含添加新员工的逻辑记录,然后使用RedirectToAction
重定向回 Index 操作。这称为后重定向获取模式。您永远不应该让您的用户“登陆”POST 响应,因为这可能会导致双重表单提交。在员工视图中:您需要上面一个表单和下面一个网格(例如 MVCContrib Grid)。表单的操作应该是
EmployeeController
的AddEmployee
操作。You need a
EmployeeController
with two Actions:Index
andAddEmployee
.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 usingRedirectToAction
. 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 theEmployeeController
.我知道这不是您想要的答案,但在我看来您正在尝试模仿 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.