views:

71

answers:

3

Hi there,

Im pretty new to ASP.NET MVC, trying to figure out my way around.

Currently i return a ViewModel which has a IEnumeable Events as its only property. This ViewModel is StronglyTyped to a UserControl which dislays the IEnumable Events in tabular form. One of the properties of the Event Model is an XElement, which contains some XML loaded from the DB.

Now i've added a link to the end of the tablular data to be able to view the XML in a separate page. How do i pass this data to another page for viewing?

A: 

There are basically two ways of bringing data from one page to another using ASP.NET MVC (or any other language/framework which follows the HTTP protocol):

  • Sessions: Use a session to store the data you need, and load it back up at the next page.
  • Post the needed data back to the server. This way, the server can hold it and display it on the next page. Posted data usually comes from input or textarea elements. If you use input type="hidden" you can give it a value which represents your data. This way, you can post it back and forth till you arrive where you want.
Arve Systad
+1  A: 

I would post a request back to the server with some sort of Id for the Event-object and have the receiving end send back the XML related to that Id.

if you're looping through the Event objects in your IEnumerable, you can do something like:

<%= Html.ActionLink("GetXml", "Events", new { id = currentEvent.Id }) %>;

Now create an Action on your EventsController (given that you have one) like so:

public ActionResult GetXml(int id)

and retrieve the XML to pass back to the View

Yngve B. Nilsen
A: 

Besides what Arve is advising, you could also consider TempData.

If you use the Get-Post-Redirect/Forward concept for you app, you could do something like:

  1. GET - Initial Request comes in, Server responds with View and model data. User selects an item which leads to ...
  2. POST - User selects one of the items from #1, triggering a post. That particular item can be fetched from repository, placed in TempData and then...
  3. REDIRECT/FORWARD - The redirect collects the information out of TenpData and uses it as the model for the new View.

here is an example http://www.eworldui.net/blog/post/2008/05/08/ASPNET-MVC-Using-Post2c-Redirect2c-Get-Pattern.aspx

Jonathan Bates