Search code examples
asp.net-mvcuser-controlsstrong-typingviewdatapartials

strongly-typed partial views MVC RC1


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);
        }

Solution

  • 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());
    }