Edit Associated Profile Entities

Current version: 8.2

Create a page using addresses within an MVC site.

Complete the following prerequisites:

At your Visual Studio Solution setup:

  1. Get all user addresses

    RequestResponse
    
    List<MVCSite.Models.Generated.Address> models = null;
    if (Sitecore.Context.User.IsAuthenticated)
    {
    	var user = Sitecore.Context.User;
    	var commerceProfile = user.GetCommerceProfileModel();
    
    	if (commerceProfile.AddressList != null)
    	{
    		models = Address.GetMultiple(commerceProfile.AddressList);
    	}
    }
    return View(models);
    
    
  2. Create or edit an address.

    RequestResponse
    
    if (model != null && Sitecore.Context.User.IsAuthenticated)
    {
    	var user = Sitecore.Context.User;
    	var profileModel = user.GetCommerceProfileModel();
    
    	if (string.IsNullOrEmpty(model.AddressID))
    	{
    		var address = Address.Create();
    
    		model.Save(address);
    
    		profileModel.AddressList.Add(model.AddressID);
    		profileModel.AddressList = profileModel.AddressList;
    		profileModel.Save();
    	}
    	else
    	{
    		if (profileModel.AddressList.Contains(model.AddressID))
    		{
    			var address = Address.GetCommerceProfile(model.AddressID);
    			if (address != null)
    			{
    				model.Save(address);
    			}
    		}
    	}
    }
    
    

Do you have some feedback for us?

If you have suggestions for improving this article,