Roy Astro
Roy Astro

Reputation: 253

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. The index page will also display the search result.

Thanks

Upvotes: 1

Views: 456

Answers (4)

Morri
Morri

Reputation:

public class CustomerController : Controller

...

public ActionResult Search(int id)
{
 ViewData["SearchResult"] = MySearchBLL.GetSearchResults(id);

 return View("Index");
}

...

Hope this helps

Upvotes: 2

Matt Hinze
Matt Hinze

Reputation: 13679

Have the HTML form post to the same action that rendered it. 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. 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.

Upvotes: 0

Adhip Gupta
Adhip Gupta

Reputation: 7163

In the URL that you suggest, you should have a Controller method called 'search' and using the 'index' view for that 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.

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421968

No, you shouldn't have. Just use View("ViewName"); in the controller to show the appropriate view in other actions.

Upvotes: 1

Related Questions