简体   繁体   English

我是否需要为ASP.NET MVC中的每个操作都拥有一个查看页?

[英]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.

相关问题 ASP.NET MVC:是否真的必须为每个动作创建一个视图? - ASP.NET MVC: Do I really have to create one view per action? asp.net mvc视图页面上有一个webform控件吗? - Can a asp.net mvc view page have a webform control on it? ASP.NET MVC如何将控制器操作指向其他视图? - ASP.NET MVC How do I point a controller action to a different view? 如何响应需要访问视图中数据的 ASP.NET MVC 应用程序页面上的事件? - How can I respond to events on a page in an ASP.NET MVC app that need to access data within the View? 如何使用asp.net mvc在视图中为控制器中的每个动作创建动作链接? - How do I create an action link for each action in my controller within a view using asp.net mvc? 使用ASP.NET MVC时如何设置新页面/视图的URL - How do I set the URL for a new page/view when using ASP.NET MVC 我可以通过jQuery将数据传递到ASP.NET MVC控制器操作并在新的浏览器选项卡中呈现视图吗 - Can I pass data via jQuery to ASP.NET MVC controller action and have a view rendered in new browser tab 如何在 ASP.NET MVC 视图中对数据进行分组? - How do I group data in an ASP.NET MVC View? 每次回发都会调用ASP.NET MVC索引操作 - ASP.NET MVC Index action gets called every postback 每次我在 ASP.NET MVC 中调用操作时都会创建数据库上下文 - DB Context is created every time I call an Action in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM