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

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

Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top