I have a search action that takes a search parameter. I have search results in a single result, and I want to redirect to a show action and if there are multiple results I want to direct to a list action with all the results listed.
I got the redirecting to work, but not quite well. I'm not sure of how to do the tr开发者_如何学JAVAansfer of search results. Should the first "search" action only extract id's and then it is up to the show or list action to extract more data based on the id´s? How should their id's be transfered to the concrete action?
I would probably have the search action render a view result with the search results if there are multiple results. It could reuse the list view, if appropriate, but it would be easier to simply display them rather than try to redirect to another action with multiple ids. In the case of a single result, redirecting to the details action for that item might be appropriate. This would mean that I would need to retrieve, during search, enough data to populate the model for a list. If redirecting to a details action, you'd simply redirect by id and let the details action get any information required for its display.
public ViewResult Search( string query )
{
... get results ..
if (results.Count() == 1)
{
return RedirectToAction( "details", new { id = results.First().ID } );
}
return View( "list", this.CreateListViewModel( results ) );
}
where CreateListViewModel takes a set of search results and produces a view model object for the List view.
The other possibility to this is to create two views, one for the single result and one for the list result and call return View("Single", model)
or View("List, collection)
.
精彩评论