[英]Do I need to have a View Page for every Action in ASP.NET MVC?
I have a customer index page that displays customer lists. 我有一个显示客户列表的客户索引页面。 I have a search functionality within this page and I want the url to be http://mysite/search?id=434 when i perform the search.
我在此页面中具有搜索功能,执行搜索时希望URL为http:// mysite / search?id = 434 。 The index page will also display the search result.
索引页面还将显示搜索结果。
Thanks 谢谢
public class CustomerController : Controller
...
public ActionResult Search(int id)
{
ViewData["SearchResult"] = MySearchBLL.GetSearchResults(id);
return View("Index");
}
...
Hope this helps 希望这可以帮助
No, you shouldn't have. 不,你不应该。 Just use
View("ViewName");
只需使用
View("ViewName");
in the controller to show the appropriate view in other actions. 在控制器中显示其他操作中的适当视图。
In the URL that you suggest, you should have a Controller method called 'search' and using the 'index' view for that controller. 在建议的URL中,应该有一个名为“搜索”的Controller方法,并对该控制器使用“索引”视图。
If thats the case, you can post it back to the same action and in the Controller have different sets of code for the 'GET' and 'POST' to give the functionality that you are looking for. 如果是这样,您可以将其发布回相同的操作,并且在Controller中针对“ GET”和“ POST”具有不同的代码集,以提供所需的功能。
Have the HTML form post to the same action that rendered it. 将HTML表单发布到呈现它的相同操作。 That action can decide if it's a non-search (first visit) or a search results rendering.
该操作可以确定是非搜索(首次访问)还是搜索结果呈现。 That action can populate
ViewData
with the appropriate data. 该操作可以用适当的数据填充
ViewData
。 That is, if you want to do that. 也就是说,如果您想这样做。
You can also have two views, really easily. 您也可以很轻松地拥有两种视图。 And the action can transparently decide which one to render.
而且该动作可以透明地决定要渲染哪个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.