开发者

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开发者_如何学Go 'object' could be found (are you missing a using directive or an assembly reference?)


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


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>"


<%@ 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>" %>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜