views:

386

answers:

2

I want to use a LinqDataSource or ObjectDataSource with ViewData.Model, where ViewData.Model is an string array.

I don't want to bind the datasource on view's PageLoad event.

Is it possible? How?

+1  A: 

You should NOT return the actual datasource into your view since that would break the whole MVC concept. Instead you should return data objects from the source via the controller in order to have a clean separation of the model internals and the view.

However if you really want to return your data source this is how you do it, it is the same way as with any kind of object you want: in your controller you return a new view with the object as a parameter. That object will become available via the view's Model property.

I.e. in your controller action have the following:

public ActionResult YourAction() {
    var yourDataSource = GetYourDataSourceMethod();
    return View(yourDataSource);
}

private LinqDataSource GetYourDataSourceMethod() { 
    // Return your datasource ... 
}

In your view you can call the Model property directly and access yourDataSource. You'll need to type cast it if you haven't typed your view like this:

<%= var myDataSource = (LinqDataSource) Model %>
Spoike
A: 

I got it! April Fool's Day, right :-)

Thomas Eyde