更正扩展方法的语法

时间:2013-08-21 12:22:35

标签: c# asp.net

我正在尝试按照示例here创建个人资料功能

让我感到震惊的是,我的web.config还包含group name来对属性进行分组。我的web.config看起来像这样

的web.config:

    <profile defaultProvider="MyCMSSqlProfileProvider" automaticSaveEnabled="false" inherits ="TestProj.Controls.wProfile">
            <providers>
                <clear/>
                <add name="MyCMSSqlProfileProvider" connectionStringName="dbMyCMSConnectionString" applicationName="MyCMS" type="System.Web.Profile.SqlProfileProvider"/>
            </providers>
            <properties>
                 <group name="Personal">
                    <add name="FirstName" type="String" />
                    <add name="LastName" type="String" />
                    <add name="Gender" type="String" />
                    <add name="BirthDate" type="DateTime" />
                    <add name="Occupation" type="String" />
                    <add name="Website" type="String" />
                </group>
                <group name="Address">
                    <add name="Country" type="String" />
                    <add name="Address" type="String" />
                    <add name="AptNumber" type="String" />
                    <add name="City" type="String" />
                    <add name="State" type="String" />
                    <add name="PostalCode" type="String" />
                </group>

//.....etc

我开始将名为ProfileInfo的类更改为

ProfileInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestProj.controls
{
    [Serializable]
    public class Personal
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string BirthDate { get; set; }
        public string Occupation { get; set; }
        public string Website { get; set; }
    }

    [Serializable]
    public class Address
    {
        public string Country { get; set; }
        public string Address { get; set; }
        public string AptNumber { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
    }


}

但知道意识到这不会起作用,因为我需要通过wProfile

返回所有组名属性

wProfile.cs:

using System.Web;
using System.Web.Profile;
namespace Project1.Account
{
    public class wProfile : ProfileBase
    {

        public ProfileInfo ProfileInfo
        { 
            get { return (ProfileInfo) GetPropertyValue("ProfileInfo"); }       
        } 

        public static wProfile GetProfile() 
        { 
            return (wProfile) HttpContext.Current.Profile; 
        } 

        public static wProfile GetProfile(string userName) 
        { 
            return (wProfile) Create(userName); 
        }  
    }
}

如何正确设置ProfileInfo.cs以公开所有属性(包括组名称)的个人资料?

在代码隐藏中,我想做类似的事情

        // Personal Info
        txtFirstName.Text = lprofile.Personal.FirstName;
        txtLastName.Text = lprofile.Personal.LastName;
        ddlGenders.SelectedValue = lprofile.Personal.Gender;
        if (lprofile.Personal.BirthDate != DateTime.MinValue)
            txtBirthDate.Text = lprofile.Personal.BirthDate.ToShortDateString();
        ddlOccupations.SelectedValue = lprofile.Personal.Occupation;
        txtWebsite.Text = lprofile.Personal.Website;

        // Address Info
        ddlCountries.SelectedValue = lprofile.Address.Country;
        txtAddress.Text = lprofile.Address.Address;
        txtAptNumber.Text = lprofile.Address.AptNumber;
        txtCity.Text = lprofile.Address.City;
        ddlStates1.SelectedValue = lprofile.Address.State;
        txtPostalCode.Text = lprofile.Address.PostalCode;

1 个答案:

答案 0 :(得分:1)

您只需要将Personal / Address作为属性添加到您的个人资料模型,即

[Serializable]
public class Personal
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public string BirthDate { get; set; }
    public string Occupation { get; set; }
    public string Website { get; set; }
}

[Serializable]
public class Address
{
    public string Country { get; set; }
    public string Address { get; set; }
    public string AptNumber { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
}

[Serializable]
public class ProfileInfo
{
    public Personal Personal { get; set; }
    public Address Address { get; set; }
}

来自docs

  

可以在用户配置文件中将属性组织为组   属性。使用该组指定配置文件属性组   配置元素。例如,用户的不同属性   地址信息可以在地址组中组合在一起。您   然后可以使用组标识符访问分组的属性   属性名称(例如,Profile.Address.Street或   Profile.Address.City)。