Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update A Partial View From Another Partial View - ASP.NET MVC2

I want to have two partial views, one for SEARCH and one for SEARCHRESULTS.

I want to update SEARCHRESULTS when the "Search" Button is clicked on the SEARCH partial view form. SEARCHRESULTS needs to have the form data fed to it from the SEARCH partial view.

I'm not totally sure how to go about this. Can I update the SEARCHRESULTS partial view from my SEARCH partial view's Controller action?

like image 433
Jason Avatar asked Sep 20 '10 02:09

Jason


1 Answers

General Discussion
In the MVC design pattern views are unaware of each other. They may be bound together by the concept of a view assembling multiple partial views but even then the partials are ignorant of each other. This concept is true for ASP.NET MVC. Mike Brind does a good job describing partials and ViewData in his post ASP.NET MVC Partial Views and Strongly Typed Custom ViewModels.

Specific to your Question
To answer your question a partial view can have a link to a controller action that renders a different view, so long as the appropriate information is passed to the controller. How you go about this will depend on what you are trying to do.

Given your question I am going to assume that the SEARCH partial view is a simple form with a search field and button. While SEARCHRESULTS is a listing of the returned data. In this instance you'd create a controller action called Search which takes in a string value and returns just the SEARCHRESULTS partial or a view containing the SEARCHRESULTS partial. Scott Guthrie provides a pretty good description of passing data to a view in his blog post Passing ViewData from Controllers to Views.

// returning partial
public ActionResult Search(string q)
{
    //do search .......
    //.................

    return PartialView("SEARCHREULTS", viewdata);
}
like image 133
ahsteele Avatar answered Oct 17 '22 12:10

ahsteele