MVC Partial Control: "cannot find an IViewDataContainer object. The ViewUserControl must be inside a ViewPage, a ViewMasterPage"

StackOverflow https://stackoverflow.com/questions/22695372

Question

I'm trying to implement SSRS viewer in MVC. After alot of pain and suffering I've got this basically working via a custom control.

The report renders fine, but I'm trying to pass in the Model to the control like this:

@Html.Partial("VendorReport", Model)

To call this ascx control:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VendorReport.ascx.cs" Inherits="ExtendedServicesReportingPortal.Views.Reports.VendorReport" %>
<uc1:RazorReportViewer ID="VendorReportViewer" runat="server" ...Omitted Custom Properties...  />

But I'm getting this error if I try and access the Model:

The ViewUserControl '~/Views/Reports/RazorReportViewer.ascx' cannot find an IViewDataContainer object. The ViewUserControl must be inside a ViewPage, a ViewMasterPage

Here's the relevant controller controller method:

public ActionResult TransactionCountsAndInvoiceAmounts(TransactionCountsAndInvoiceAmountsModel model)
    {
        if(model == null) model = new TransactionCountsAndInvoiceAmountsModel();

        return View(model);
    }

Very new to MVC, what am I missing out?

Was it helpful?

Solution

I think the issue here is that where you are using a User Control the ViewPage.Model property will be null since the control does not have access to/use of ViewData. You will likely need to pass the model into the user control.

There are some examples here and also here that show reusing Web Form user controls in MVC views including passing in the model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top