views:

175

answers:

2

I'm new to ASP.NET MVC, but I need to perform a search for articles that match a selected category. The results of this query need to be written into a "search results" div overlay with DHTML- jquery, probably.

So, I need the results of an Action, but not to render a view. I was thinking I could use Json and then iterate over the resulting records, somehow.

Or is it easier to use RenderPartial... but how would I use this in this DHTML scenario?

Thanks.

A: 

Hmmm, sounds like you are trying to do a filter?

If this is the case I think it's a bad idea trying to search within your html. I think a better approach would be to post back using jQuery, get your result set from whatever database you are using, and return that back to the view as apartial view.

When you are searching your database you should apply the filter at that point using sql, Linq2Sql or whatever you're using.

If you are still Hell bent on searching the HTML then I'd give each relevant div a class name of say class="DivSearchable". Then in jQuery you can do something like;

$('.DivSearchable").each(function() { 

 var text = $(this).val();
 "Now test text for contents of your seach string"
  if (BoolIfFound == true)
    $(this).addClass("highlightClassName");
});

highlightClassName would set the background-color to something so you can see which divs contains the search string.

make sense?

griegs
Yes, I'd be using linq to query a SQL Server DB... something to the effect of (x=>x.Category == categoryVal).But can I use a partial view if I'm not reloading the current page? I'm staying on the same page, and using ajax to get the results and paint it into a results div.
Scott
yes you can. A partial view can be bound to a class or to a List<YourClass> which is the prefered approach in this case. Then in your controller you would return PartialClass("PartialClassName", List<YourClass>); What that does is, as far as you need to be concerned with, a bunch of HTML. In your success of the jQuery call, you would then replace the html contents of say a bound div with the new returned html. Job done! Hope this makes sense.
griegs
Sorry that should read "html contents of a bounding div". So a div that contains all the rest of the divs comprising your content. Give the bounding div a class name and use $('.YourBoundingDivClassName').html(NEWHTML);
griegs
+1  A: 

I like the way Steve Sanderson describes in his ASP.NET MVC book. It dosnt work with JSON, but returns a partial. This makes it easier to have both: An Ajax and a non-Ajax version.

The cotroller returns a View or Partial depending on the type of request:

public ActionResult GetArticles(string category)
{
    ...
    if(Request.IsAjaxRequest())
    {
     return PartialView("ArticleListPartial",articleModel)
    }
    else
    {
     return View("ArticleListPage",articleModel)
    }
}

The search by default submits the from with with a Non-Ajax post:

<form id="articleSearch" method ="post" action="/Article/GetArticles" >
...
<input type="submit" value="Get the articles!" />
<form>

Then there is a Jquery snippet that kicks in when Javascript is available and submits the request via Ajax

<script language="javascript" >
  $(function() {
    $("#articleSearch").submit(function() {
     $.post($(this).attr("action"), $(this).serialize(), function(modelResponse) {
      ("#articleResultContainer").html(modelResponse);

     });
     return false;
    });
  });
</script>
Malcolm Frexner