views:

1570

answers:

5

having a problem passing ViewData.Model to the partial views. It always is defaulting to null even if I equate it to a result query. I cannot access the strongly typed data because the Model is null. My current code is this,

ViewPage

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial("header", this.ViewData.Model); %>
    <% Html.RenderPartial("test", this.ViewData.Model); %>
    <div id="userControls">
    </div>
</asp:Content>

UserControl - header

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<testMVCProject.Models.information>" %>
<h2>
    ACReport</h2>
<p>
    id:
    <%= Html.Encode(Model.id) %>
</p>
<p>
    type:
    <%= Html.Encode(Model.type) %>
</p>

UserControl - test

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<testMVCProject.Models.information>" %>

        <%  using (Ajax.BeginForm(
            "pressureV2",
            "Home",
            new { id = ViewData.Model.id },
            new AjaxOptions
            {
                UpdateTargetId = "userControls",
                HttpMethod = "GET"

            },
            new { @id = "genInfoLinkForm" }))
            {%>
        <%= Html.SubmitButton("hey", "Lol") %>

    <%} %>

Controller

public ActionResult header(int id)
        {
            var headerResults = from c in db.information
                                where c.id == id
                                select new information
                                {
                                    id = c.id,
                                    type = c.type
                                };
            ViewData.Model = headerResults.FirstOrDefault();
            return View(ViewData.Model);
        }

public ActionResult pressureV2(int id)
        {
            var pressureVResults = from c in db.pressure_volume_tests
                                   where c.id == id
                                   select new pressureVT
                                   {
                                       bottomCVP = c.bottom_CVP,
                                       topCVP = c.top_CVP
                                   };

            ViewData.Model = pressureVResults.FirstOrDefault();
            return View(ViewData.Model);
        }
A: 

I believe the problem might be that you're missing an element in the form with the name "id" so the parameter of the Action method is never populated with a value?

That way the query would always return null with the FirstOrDefault, hence the null Model.

Just my guess...

antonioh
A: 

Have you tried making the ViewPage generic as well?

eulerfx
+2  A: 

In the comments you have said that the view is not strongly typed. Because of that:

<% Html.RenderPartial("header", this.ViewData.Model); %>
<% Html.RenderPartial("test", this.ViewData.Model); %>

will not work. If you strongly type your view to testMVCProject.Models.information and then pass an instance of that type from your constructor it will work.

Controller:

public ActionResult ShowAView()
{
    Return View("WhateverYourViewIsCalled", new information());
}
liammclennan
since I'm using partials all over my view if I wanted to add new query information to the partials am I restricted to only what is being passed from the viewpage itself or can i restrict to only be within the partial?
Ayo
Partials can only get their data from the view. See Justin's answer.
liammclennan
A: 

The Controller doesn't get called when you RenderPartial - it is bypassed and the view is rendered directly. So whatever you want to pass in as a model needs to be done from the calling View.

Justin
where did you get this information from, I'm pretty sure I can access the controller when calling the renderpartial method
Ayo
From personal experience. When you call RenderPartial("viewName"), the view engine will not attempt to call a controller method - it will look for a view called "viewName". Go try it...You might be able to override the default view engine to use controllers...?
Justin
+1  A: 

Hi,

You have a misunderstanding of the use of Html.RenderPartial helper. When you use the RenderPartial you will show the view without requesting the model from the controller.

So you have to refactor your ViewPage and pass the good Model to your usercontrols:

Exemple:

Controller:

ActionResult MainView()
{
    var mainviewobj = new MainViewObject();

    var headerResults = from c in db.information
                                where c.id == id
                                select new information
                                {
                                    id = c.id,
                                    type = c.type
                                };

    mainviewobj.info = headerResults.FirstOrDefault();

    return view(mainviewobj);  
}

View Code:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial("header", this.ViewData.Model.info); %>
    <% Html.RenderPartial("test", this.ViewData.Model.info); %>
    <div id="userControls">
    </div>
</asp:Content>

View Code Behind

public partial class MainView : ViewPage<MainViewObject>
{
}

Now the Model will not be null in your usercontrol. But remember the usercontrol rendering partially dun execute the code in the controller So you dun need the public ActionResult header(int id) in your Controller

Hope this helps.

alexl
im tryingto avoid codebehind files and Im not sure if this appraoch will work for dynamic Model overloads
Ayo