Blankman
Blankman

Reputation: 267010

Html.RenderParial, how to pass and use the model?

I am doing this:

<% Html.RenderPartial("SomePartial", Model); %>

My Model has a property UserID

In my partial I try this:

<%= Model.UserID %>

but I get this error:

CS1061: 'object' does not contain a definition for 'UserID' and no extension method 'UserID' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 0

Views: 103

Answers (4)

jim tollan
jim tollan

Reputation: 22485

Blankman,

Make sure your partial ascx file defines the model as per the main view i.e:

<%@  Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Your.Model>" %>

[edit] - as Stefanvds mentions below, if you only need the id portion from the model, then define your partial as:

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

that would be the definitive i reckon :)

cheers

Upvotes: 2

Dave D
Dave D

Reputation: 8972

Make sure that at the top of your partial view you have Inherits attribute of your Control tag set to the type of the model you're passing in:

Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.MyStronglyTypedModel>"

Upvotes: 1

Arief
Arief

Reputation: 6085

The model type on your Partial View and the model you pass in to Html.RenderPartial method should match.

Upvotes: 0

Stefanvds
Stefanvds

Reputation: 5916

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

your partial should start with this.

also, if you want to send the whole model, you dont need to specify it.

<% Html.RenderPartial("SomePartial"); %>

or if you would only like to send the ID.

<% Html.RenderPartial("SomePartial", Model.UserID); %>

which would make the header

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

Upvotes: 0

Related Questions