views:

227

answers:

2

Hi Guy's,

Im using a custom profile class within my mvc app but i cannot access a property in my view.

Here is some code

Web.config

<profile inherits="ValuePack.Helpers.ProfileCommon" defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">    <providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="_427388_valuepackConnectionString" applicationName="/" />
</providers>
</profile>

ProfileCommon

using System.Web.Profile;
using System.Web;
using System.Web.Security;

namespace ValuePack.Helpers
{
public class ProfileCommon : ProfileBase
{
    public virtual string company
    {
        get { return ((string)(this.GetPropertyValue("Company"))); }
        set { this.SetPropertyValue("Company", value); }
    }

    public virtual ProfileCommon GetProfile(string username)
    {
        return ((ProfileCommon)(ProfileBase.Create(username)));
    }

}
}

Controller

public ActionResult Members()
{
List<aspnet_User> members = new List<aspnet_User>(dr.GetAllMembers());
return View(members);
}

View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ValuePack.Models.aspnet_User>>" %>

<h2>Members</h2>
<table>
<tr>
    <th>UserId</th>
    <th>Company<th>
    <th>LastActivityDate</th>
    <th></th>
</tr>
<% foreach (var item in Model)
   { %>
<tr>
    <td><%= Html.Encode(item.UserId) %></td>
    <td><%= ValuePack.Helpers.ProfileCommon.GetProfile(item.UserName).company %></td>
    <td><%= Html.Encode(String.Format("{0:g}", item.LastActivityDate)) %></td>
    <td><%= Html.ActionLink("View", "Details", new { id=item.UserId })%></td>
</tr>
<% } %>
</table>

As you can see im trying to access a profile property in my view but it throws the following error...

CS0120: An object reference is required for the non-static field, method, or property 'ValuePack.Helpers.ProfileCommon.GetProfile(string)'

Any ideas?

Thanks

A: 

The GetProfile method in your helper class should be declared static in order to be accessed in the manner you are using, otherwise you would need to instantiate a ProfileCommon object, then call the GetProfile method on it. The latter is almost certainly not what you want.

public static ProfileCommon GetProfile(string username)
{
   ...
}
tvanfosson
A: 

Thank You! How do i mark as answer?

Dooie
Just click the check mark to the left of the correct answer.
Craig McKeachie