4

I have a search page that if there is results in the list it passes this list to a view. However if there are no results I want to send the searched text to a no results found view. How would I go about this?

7 Answers 7

6

You will need to have the searched text available as part of the model that is returned to the view. Then you have two options -

Using the RenderPartial will pass the returned view to the partial view so you can access the value you want from there.

Html.RenderPartial("PartialView");

Alternatively, you can pass the string as the model for the partial view using

Html.RenderPartial("PartialView", Model.SearchedText);

Which might make sense if you want to use the no results partial view with different models.

Sign up to request clarification or add additional context in comments.

1 Comment

I had to add it as part of the model. Now I need to refactor my code :)
3
<%Html.RenderPartial("SimpleTrustGridViewer", ViewData["departmentGrid"]); %>

this passes an object ViewData["departmentGrid"] (this comes from viewdata of the non-partial view) to the partial view SimpleTrustGridViewer.

simplified:

<%Html.RenderPartial("myUserControl", myString); %>

And your partial view inherits like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>

Then, in your partial view 'Model' will be the passed string.

Comments

1

The ViewDataDictionary passed from the controller to the view will be the same passed from the View to the Partial View. So if the string you want to pass is in the ViewDataDictionary you don't have to pass it.

<%=Html.RenderPartial("NorResultFound")) %>  

But you can use the same view whether there were results or not:

<%if (Model.ResultCount!=0){ %>
<%foreach(var result in Model){ %>
<%= // display results %>
<%}}%>

<%else {%>
<p>There is no results for <%=ViewData["keyword"]%> </p>
<%} %>

1 Comment

I agree with you, but i prefer using a strongly typed model instead mixin' ViewData and model. My model would have 2 props (refactoring friendly code:D), class SearchResult { public IList<Result> List {get; set;} public string Query {get; set;} }
1

I tried this and couldn't get it to work. Say I have

 <div id="SearchBar"> 


<% using (Html.BeginForm("IndexNoJavaScript", "Home"))  
{%>  
<%= Html.TextBox("SearchTextBox", ViewData["SearchText"]) %>
  <input type="submit" value="Search" />  <% } %>
  </div> 
 <% Html.RenderPartial("SearchResults"); %>

And when I try to display the search text in this view like so:

<%= Html.TextBox("SearchedText", ViewData["SearchText"] ) %>

My text box is blank.

Comments

1

You can use jquery and load() action on div tag , insted of using partial; the result is similar. The load() ajax method call on controller with the text that you want. like:

$('#divId').load('url/'+ serch content );

Comments

0

Two ways (you are talking about views, not partial views right?) 1) in your controller just call a different view in case of no results passing a string as model 2) create a model containing a search status (found x items, no match found, etc...) and a list of results to the same view, allowing the view to render the different results with a switch statement.

Comments

0

Partial view unless you pass to it something else explicitly, has the same Model as parent view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.