从另一个部分视图更新部分视图 - ASP.NET MVC2
我想要两个部分视图,一个用于SEARCH
,另一个用于SEARCHRESULTS
。
我想在 SEARCH
部分视图表单上单击“搜索”按钮时更新 SEARCHRESULTS
。 SEARCHRESULTS
需要从 SEARCH
部分视图提供表单数据。
我不太确定该怎么做。我可以通过 SEARCH
部分视图的控制器操作更新 SEARCHRESULTS
部分视图吗?
I want to have two partial views, one for SEARCH
and one for SEARCHRESULTS
.
I want to update SEARCHRESULTS
when the "Search" Button is clicked on the SEARCH
partial view form. SEARCHRESULTS
needs to have the form data fed to it from the SEARCH
partial view.
I'm not totally sure how to go about this. Can I update the SEARCHRESULTS
partial view from my SEARCH
partial view's Controller action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般性讨论
在 MVC 设计模式中,视图彼此不知道。它们可以通过组合多个局部视图的视图的概念结合在一起,但即使如此,局部视图也彼此不了解。这个概念对于 ASP.NET MVC 来说是正确的。 Mike Brind 在他的文章中很好地描述了部分和 ViewData ASP.NET MVC 部分视图和强类型自定义 ViewModel。
针对您的问题
要回答您的问题,只要将适当的信息传递给控制器,部分视图就可以具有指向呈现不同视图的控制器操作的链接。你如何去做这将取决于你想要做什么。
鉴于您的问题,我将假设
SEARCH
部分视图是一个带有搜索字段和按钮的简单表单。而SEARCHRESULTS
是返回数据的列表。在本例中,您将创建一个名为Search
的控制器操作,它接受一个字符串值并仅返回SEARCHRESULTS
部分或包含SEARCHRESULTS
的视图> 部分。 Scott Guthrie 在他的博客文章 将 ViewData 从控制器传递到视图。General Discussion
In the MVC design pattern views are unaware of each other. They may be bound together by the concept of a view assembling multiple partial views but even then the partials are ignorant of each other. This concept is true for ASP.NET MVC. Mike Brind does a good job describing partials and ViewData in his post ASP.NET MVC Partial Views and Strongly Typed Custom ViewModels.
Specific to your Question
To answer your question a partial view can have a link to a controller action that renders a different view, so long as the appropriate information is passed to the controller. How you go about this will depend on what you are trying to do.
Given your question I am going to assume that the
SEARCH
partial view is a simple form with a search field and button. WhileSEARCHRESULTS
is a listing of the returned data. In this instance you'd create a controller action calledSearch
which takes in a string value and returns just theSEARCHRESULTS
partial or a view containing theSEARCHRESULTS
partial. Scott Guthrie provides a pretty good description of passing data to a view in his blog post Passing ViewData from Controllers to Views.