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?)
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.
Related
Developers, I am in need of providing different
<%# Eval("variable") %>
For my Grid. I have requirements that require me to either show StagingDB Data or ProdDB Data per Eval
Pseudocode
<%# If Eval("isDraft) ? Eval("tbl_staging_value") : Eval("tbl_value") %>
Stackoverflowers... You are my only hope... Thanks. Working on Sunday...
You can use ?: operator.
<%# Convert.ToBoolean(Eval("isDraft")) ? Eval("tbl_staging_value"):
Eval("tbl_value") %>
By creating a new property on your data source
public string tbl_val
{
get
{
return isDraft ? tbl_staging_value : tbl_value;
}
}
you can simplify the data binding
<%# Eval("tbl_val") %>
This is how a register the control on the page:
<%# Register TagPrefix="uc" TagName="Pcp" Src="../../Controls/ClientPayement.ascx" %>
And this is how I use it:
<uc1:Pcp ID = "pcpClient1"></uc1:Pcp>
I don't know why uc1 has a green underline warning. When I hover over it, it says: "Unrecognized namespace uc1". And the user control is not displaying on the page.
Am I missing something? Yet this tutorial says that this is all I need to do.
You are specifying a uc prefix in <%# Register TagPrefix="uc" .. so that's the one you should use:
<uc:Pcp ID = "pcpClient1"></uc:Pcp>
Or you could change the declared prefix:
<%# Register TagPrefix="uc1" TagName="Pcp" Src="../../Controls/ClientPayement.ascx" %>
And use the control as you posted in the question:
<uc1:Pcp ID = "pcpClient1"></uc1:Pcp>
EDIT
As Shai Cohen pointed out in another answer, you are also missing the runat="server" from the usercontrol tag.
<uc1:Pcp ID = "pcpClient1" runat="server"></uc1:Pcp>
In addition to Juan's answer, you are also missing runat="server".
<%# OutputCache Duration="10" VaryByParam="Button1" %>
I type the above code to vary text of Button but it does not vary the value .
VaryByParam refers to query string parameters. Not the value of buttons or other controls.
For example:
http://www.example.com/mypage.aspx?id=10
http://www.example.com/mypage.aspx?id=20
Would be cached separately if you set:
<%# OutputCache Duration="10" VaryByParam="id" %>
The way this page is laid out, all of the data is loaded at Page_Init. Well, I have a custom control that is having problems with this though.
I have it on the page like so:
<cc:SomeControl... />
And then I set the value at Page_Init using
MyControl.Value="blah";
Simple stuff..
The Value is an accessor and has something similar to this:
public string Value{
get...
set{
EnsureChildControls();
MyHiddenField.Value=value;
}
}
and it is here that I have a problem. It says that MyHiddenField is null. Is Page_Init just too early for this? Or is there some other function I need to call?
The fix for this was changing from using a namespace to reference the CustomControl to using a src with a filename
changing this:
<%# Register Assembly="MyProduct" Namespace="MyProduct.CustomControls" TagPrefix="cc" %>
to this:
<%# Register src="/CustomControls/MyControl.ascx" tagname="MyControl" tagprefix="uc2" %>
normally when i want to dislplay the value of a dataitem in a repeater, i use this:
<%#Eval("contact") %>
but now i want to transform the value of this field (contact) in a codebehind function, so i tried this:
<%= ShowcontactInfo(Eval("Contact")) %>
but then i get the exception (at runtime):
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
What can i do?
Michel
Does this not work?
<%# ShowcontactInfo(Eval("Contact")) %>
Note that Eval returns an object, so your method ShowcontactInfo either needs to aceept an object, or you need to use a cast.
<%= is a shortcut for Response.Write, and is not the same as thnhe databinding syntax <%#
Can't you do it like this? : <%# ShowcontactInfo(Eval("Contact")) %>