tags:

views:

556

answers:

2

How create View page which is composed of three partial view pages? I am using ASP.NET MVC

    //
    // GET: /Partial/

    public ActionResult View1()
    {
        var upit = from i in proba.name
                   select i;
        return PartialView("upit",proba.name);
    }

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>
<div><%Html.RenderPartial("View1",proba.name); %></div>
</asp:Content>

Why this code return error: Object reference not set to an instance of an object.

+1  A: 

Inside your view page you would want to make use of the RenderPartial method.

Example

Say I had 3 partial views called "View1", "View2" and "View3". If I wanted to render all 3 of these views to make up the content of my view then I would do something like:

<div id="section1">
<% Html.RenderPartial("View1", Model.Table1) %>
</div>
<div id="section2">
<% Html.RenderPartial("View2", Model.Table2) %>
</div>
<div id="section3">
<% Html.RenderPartial("View3", Model.Table3) %>
</div>

I assume you would have a MasterPage which your view derives from in order to take care of the other necessary markup. The above would just be placed inside the Content section of your derived view.

James
This work properly if view1, view2 and view3 strongly typed view from one data table, but I have error if View1 created from one table, View2 from other data table and view3 also from other data table..why?
Ognjen
You would have to pass in the correct model for whatever model type your view requires. See updated example
James
I updated my code but have one error
Ognjen
@ognjenb: The reason you are getting the exception is because `proba` is not a variable. It is used within the context of the LINQ query only. In your case, you would pass `upit` into the View as that is where your query is saved.
James
+1  A: 

I think you want to use RenderAction, not RenderPartial. View1 is an action, not a view. It returns the partial view, upit. Note that this requires MVC2 (or MVC1 + futures). You also probably want to decorate the action with the ChildActionOnlyAttribute unless you intend to call it from AJAX as well.

[ChildActionOnly]
public ActionResult View1()
{
    var upit = from i in proba.name
               select i;
    return PartialView("upit",proba.name);
}


asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>
<div><%= Html.Action("View1"); %></div>
</asp:Content>

The reason you are getting the specific error is that the proba variable isn't defined in the view, nor does it need to be. In your case, the action determines the model being passed to the partial view so there's no need for any data to be passed. You could only pass it via query parameters anyway when rendering an action.

tvanfosson
In your case I have this error: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
Ognjen
@ognjenb: I used the version that wrote directly to the response rather than the version that returns a string. I've updated it to use the correct extension method. I could also have simply changed the tag to the one that doesn't wrap the response with a write, too.
tvanfosson