views:

134

answers:

3

A function returns only one view. what if I want to return multiple views in a function?

For example, I have this code:

Function Index() As ActionResult

    Dim _news As DataTable = News.newsSelect()
    Dim _announcement As DataTable = Announcement.SelectAnnouncement()

    Return View()
End Function

I want to return _news and _announcement to be used in the aspx page. How would I do this?

A: 

Hi,

One simple way is just to have those two datasets sent in a ViewData element, which you can access in a field.

example:

 ViewData["Elements"] = new SelectList(aElements, "Guid", "Name");

is consumed as:

  <%= Html.DropDownList("Elements","Pick an element")%>

Also, I think that if you read between the lines of this blog post here you will find an elegant way of achieving what you want ;) but its a bit more involved..(only because you mentioned Views instead of just variables..

Quote:

  1. We need to create our own implementation of IViewFactory. This is responsible for locating and creating an instance of an IView (which both ViewPage and ViewUserControl implement).

  2. To “inject” (all you DI fans excuse me borrowing the term without using a DI framework) our new View Factory into every Controller we are going to create our own IControllerFactory implementation.

  3. We need to configure the framework to use our new Controller Factory.

  4. Finally we can create two Views – an AJAX version and a pure HTML version.

Building on that should be all you need

Good luck!

Ric

Ric Tokyo
+2  A: 

Are you trying to show both sets at the same time? News and Announcements?

If so then why not implement either a PartialView or two PartialViews?

Then in your main view you can render them and pass the collection to the PartialViews?

There are heaps of samples on this and the one I recommend is in NerdDinner if you haven't already seen it.

I hope this helps. If you want sample code then let me know.

griegs
A: 

Assuming what you are trying to do is use both of those DataTables to populate some View, then my recommendation would be to create a wrapper object and then a strongly typed view based on this object.

The wrapper object would contain properties for all of the data elements that you need in order to render your view properly. In your case, it is 2 DataTable objects. I do not really know VB, so all my examples will be in C#. Here is an example of the data wrapper class...

public class IndexViewData
{
    public DataTable News { get; set; }
    public DataTable Announcement { get; set; }
}


You then might update the Index action in your controller as follows:

public ActionResult Index()
{
    var viewData = new IndexViewData();
    viewData.News = News.newsSelect();
    viewData.Announcement = Announcement.SelectAnouncement();

    return View(viewData);
}


Finally, you would need to create/update your view to be strongly typed. This is done by having your page inherit from the generic System.Web.Mvc.ViewPage<T> class. Just substitute the view data wrapper created earlier for T. To do this, you would set the inherits attribute of the <%@ Page %> element. In your case, if we assume your root namespace is called "Foo", you might have the following page declaration in your Index.aspx view (added extra line breaks for readability):

<%@ Page Title=""
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage<Foo.Models.MyModelType.IndexViewData>"
%>


Once you have a strongly typed view for your view data wrapper, you can access the wrapper object in your view using the Model property. Here is an example of something you could do in your Index.aspx view

<%-- Output some random data (bad example, for demonstration only) --%>
<%= Model.News[0]["title"] %><br/>
<%= Model.Anouncement[0]["body"] %>


In reality you're probably going to do something like iterate over each row of the data table. Regardless, once you create the strongly typed view, your model object, which was passed to the view in the Index method of the controller, is available in the Model property within the view.

You can find detailed tutorials at the ASP.NET MVC site

Justin Holzer