动作-视图绑定:神奇!
假设有一个具有详细信息操作的 HomeController。 return View() 会将数据发送到 Home 文件夹中的 Detals.aspx。但谁来进行这种约束呢?如果我希望它转到 Edit.aspx 该怎么办?
背景: Details.aspx 和 Edit.aspx 中的许多代码都是相同的,除了一个文本框。也许按照 MVC 的严格性,视图不应该做出这样的决定,但是,嘿,必须有一个限制。
Say there's HomeController with an Details-action. return View() will send data to the Detals.aspx in the Home folder. But who makes that binding? and what if I want it to go to Edit.aspx instead?
Background:
Alot of the code in Details.aspx and Edit.aspx is identical, save for one textbox. Maybe by MVC rigor, the view is not supposed to make that kind of decisions, but hey, there's got to be a limit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将其指定为
View()
函数的参数来使其转到 Edit.aspx。至于谁来进行实际的绑定,那就是视图引擎。它接收返回的 ViewResult 并对其进行分析以查看要加载和显示哪个模板文件。当它获取字符串“Edit”时,它会使用控制器的上下文运行查找例程,以在多个目录中搜索与约定匹配的文件名。它从控制器的 View 目录开始,然后搜索 Shared 目录。
You can make it go to Edit.aspx by specifying it as a parameter of the
View()
function.As to who makes the actual binding happen, it's the View Engine. It receives the returned ViewResult and analyzes it to see which template file to load and display. When it gets the string "Edit", it runs a find routine, using the context of the controller, to search a number of directories for filenames that match the convention. It starts in the controller's View directory, and then searches the Shared directory.
如果您想要呈现
Edit.aspx
,您可以return View("Edit");
If you want to
Edit.aspx
to be rendered you couldreturn View("Edit");