How to add fields to the registration 2.10

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi everyone!,
i'm trying to add fields to the registration form, i was using this tutorial http://blog.csharpwebdeveloper.com/2011/09/10/updating-an-existing-entity-in-nopcommerce-2-0/ and i have added a field to the database, in the register the textbox apear, but wen the register button is pressed the field in the database is empty and the person is registered.

if anyone has an idea, please share it.
THNKZ.
12 years ago
I suggest to use this codeplex changeset and make those changes to your project. You will get a positive result.
12 years ago
Guerrerohgp wrote:
Hi everyone!,
i'm trying to add fields to the registration form, i was using this tutorial http://blog.csharpwebdeveloper.com/2011/09/10/updating-an-existing-entity-in-nopcommerce-2-0/ and i have added a field to the database, in the register the textbox apear, but wen the register button is pressed the field in the database is empty and the person is registered.

if anyone has an idea, please share it.
THNKZ.


It is possible that you didn't map the view model to your domain object when the form was submitted and before it was persisted in the database. You should review the changes you made inside of the controller to ensure the proper mappings occur. To get a better idea of your changes it would be helpful if you could share the following three items.

1. View model, with the change called out.
2. Domain Mapping, with the change called out.
3. Controller action code that is executed when the registration form is submitted.
12 years ago
ok, in the view i've added this:
Nop/Web/View/Custommer/Register.cshtml

<tr class="row">
                            <td class="item-name">
                                @Html.LabelFor(model => model.Title):
                            </td>
                            <td class="item-value">
                                @Html.EditorFor(model => model.Title)
                                @Html.ValidationMessageFor(model => model.Title)
                            </td>
                        </tr>


the map :
nop.core.domain.customers.customer.cs

i've added this line in the posicion 168.
        public virtual string Title { get; set; }


the register action in customer:



        [HttpPost]
        [CaptchaValidator]
        public ActionResult Register(RegisterModel model, bool? captchaValid)
        {
            //check whether registration is allowed
            if (_customerSettings.UserRegistrationType == UserRegistrationType.Disabled)
                return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Disabled });
            
            if (_workContext.CurrentCustomer.IsRegistered())
            {
                //Already registered customer.
                _authenticationService.SignOut();
                
                //Save a new record
                _workContext.CurrentCustomer = _customerService.InsertGuestCustomer();
            }
            var customer = _workContext.CurrentCustomer;


            if (captchaValid.HasValue && !captchaValid.Value)
                ModelState.AddModelError("", _localizationService.GetResource("Common.WrongCaptcha"));

            if (ModelState.IsValid)
            {
                bool isApproved = _customerSettings.UserRegistrationType == UserRegistrationType.Standard;
                var registrationRequest = new CustomerRegistrationRequest(customer, model.Email,
                    _customerSettings.UsernamesEnabled ? model.Username : model.Email, model.Password, PasswordFormat.Hashed, isApproved);
                var registrationResult = _customerService.RegisterCustomer(registrationRequest);
                if (registrationResult.Success)
                {
                    //properties
                    if (_dateTimeSettings.AllowCustomersToSetTimeZone)
                        customer.TimeZoneId = model.TimeZoneId;
                    //VAT number
                    if (_taxSettings.EuVatEnabled)
                    {
                        customer.VatNumber = model.VatNumber;
                        
                        string vatName = string.Empty;
                        string vatAddress = string.Empty;
                        customer.VatNumberStatus = _taxService.GetVatNumberStatus(customer.VatNumber, out vatName, out vatAddress);
                        //send VAT number admin notification
                        if (!String.IsNullOrEmpty(customer.VatNumber) && _taxSettings.EuVatEmailAdminWhenNewVatSubmitted)
                            _workflowMessageService.SendNewVatSubmittedStoreOwnerNotification(customer, customer.VatNumber, vatAddress, _localizationSettings.DefaultAdminLanguageId);

                    }
                    //save
                    _customerService.UpdateCustomer(customer);

                    //form fields
                    if (_customerSettings.GenderEnabled)
                        _customerService.SaveCustomerAttribute(customer, SystemCustomerAttributeNames.Gender, model.Gender);
                    _customerService.SaveCustomerAttribute(customer, SystemCustomerAttributeNames.FirstName, model.FirstName);
                    _customerService.SaveCustomerAttribute(customer, SystemCustomerAttributeNames.LastName, model.LastName);
                    if (_customerSettings.DateOfBirthEnabled)
                    {
                        DateTime? dateOfBirth = null;
                        try
                        {
                            dateOfBirth = new DateTime(model.DateOfBirthYear.Value, model.DateOfBirthMonth.Value, model.DateOfBirthDay.Value);
                        }
                        catch { }
                        _customerService.SaveCustomerAttribute(customer, SystemCustomerAttributeNames.DateOfBirth, dateOfBirth);
                    }
                    if (_customerSettings.CompanyEnabled)
                        _customerService.SaveCustomerAttribute(customer, SystemCustomerAttributeNames.Company, model.Company);
                    if (_customerSettings.NewsletterEnabled)
                    {
                        //save newsletter value
                        var newsletter = _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmail(model.Email);
                        if (newsletter != null)
                        {
                            if (model.Newsletter)
                            {
                                newsletter.Active = true;
                                _newsLetterSubscriptionService.UpdateNewsLetterSubscription(newsletter);
                            }
                            else
                                _newsLetterSubscriptionService.DeleteNewsLetterSubscription(newsletter);
                        }
                        else
                        {
                            if (model.Newsletter)
                            {
                                _newsLetterSubscriptionService.InsertNewsLetterSubscription(new NewsLetterSubscription()
                                {
                                    NewsLetterSubscriptionGuid = Guid.NewGuid(),
                                    Email = model.Email,
                                    Active = true,
                                    CreatedOnUtc = DateTime.UtcNow
                                });
                            }
                        }
                    }
                    if (_customerSettings.ShowCustomersLocation)
                    {
                        _customerService.SaveCustomerAttribute(customer, SystemCustomerAttributeNames.LocationCountryId, model.LocationCountryId);
                    }

                    //login customer now
                    if (isApproved)
                        _authenticationService.SignIn(customer, true);

                    //associated with external account (if possible)
                    TryAssociateAccountWithExternalAccount(customer);
                    

                    //notifications
                    if (_customerSettings.NotifyNewCustomerRegistration)
                        _workflowMessageService.SendCustomerRegisteredNotificationMessage(customer, _localizationSettings.DefaultAdminLanguageId);
                    
                    switch (_customerSettings.UserRegistrationType)
                    {
                        case UserRegistrationType.EmailValidation:
                            {
                                //email validation message
                                _customerService.SaveCustomerAttribute(customer, SystemCustomerAttributeNames.AccountActivationToken, Guid.NewGuid().ToString());
                                _workflowMessageService.SendCustomerEmailValidationMessage(customer, _workContext.WorkingLanguage.Id);

                                //result
                                return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.EmailValidation });
                            }
                            break;
                        case UserRegistrationType.AdminApproval:
                            {
                                return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.AdminApproval });
                            }
                            break;
                        case UserRegistrationType.Standard:
                            {
                                //send customer welcome message
                                _workflowMessageService.SendCustomerWelcomeMessage(customer, _workContext.WorkingLanguage.Id);

                                return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Standard });
                            }
                            break;
                        default:
                            {
                                return RedirectToAction("Index", "Home");
                            }
                            break;
                    }
                }
                else
                {
                    foreach (var error in registrationResult.Errors)
                        ModelState.AddModelError("", error);
                }
            }

            //If we got this far, something failed, redisplay form
            model.AllowCustomersToSetTimeZone = _dateTimeSettings.AllowCustomersToSetTimeZone;
            foreach (var tzi in _dateTimeHelper.GetSystemTimeZones())
                model.AvailableTimeZones.Add(new SelectListItem() { Text = tzi.DisplayName, Value = tzi.Id, Selected = (tzi.Id == _dateTimeHelper.DefaultStoreTimeZone.Id) });
            model.DisplayVatNumber = _taxSettings.EuVatEnabled;
            //form fields
            model.GenderEnabled = _customerSettings.GenderEnabled;
            model.CompanyEnabled = _customerSettings.CompanyEnabled;
            model.NewsletterEnabled = _customerSettings.NewsletterEnabled;
            model.DateOfBirthEnabled = _customerSettings.DateOfBirthEnabled;
            model.UsernamesEnabled = _customerSettings.UsernamesEnabled;
            model.LocationEnabled = _customerSettings.ShowCustomersLocation;
            model.DisplayCaptcha = _captchaSettings.Enabled;
            if (_customerSettings.ShowCustomersLocation)
            {
                foreach (var c in _countryService.GetAllCountries())
                {
                    model.AvailableLocations.Add(new SelectListItem() { Text = c.Name, Value = c.Id.ToString(), Selected = (c.Id == model.LocationCountryId) });
                }
            }

            return View(model);
        }



thanks.
12 years ago
Guerrerohgp,

It was incomplete mapping like I mentioned above. In the action, roughly around line 286:


                    //save
                    _customerService.UpdateCustomer(customer);


You should have


//save
customer.Title = model.Title
_customerService.UpdateCustomer(customer);


That should get registration working as you expect.
12 years ago
thanks, but it does not work. another idea?
12 years ago
Guerrerohgp wrote:
thanks, but it does not work. another idea?


Can you explain how it doesn't work? Below is a list of places to verify. Be sure and check each one carefully.

1. Ensure that the RegisterModel has a property name "Title".
2. customer.Title = model.Title must be mapped before update.
3. CustomerMap must have a mapping for the new property. It would look something like "Propert(m => m.Title)".

That should be all that is required to save the value into the database.
12 years ago
Hello

How can i show product list which has been already searched in search box  on home page???
am using nopcommerce 2.1 version



plz help me

Thank You....
12 years ago
Jayashree wrote:
Hello

How can i show product list which has been already searched in search box  on home page???
am using nopcommerce 2.1 version



plz help me

Thank You....


Hi Jayashree,

This should be posted in a new topic as it does not relate to adding fields in registration. Thanks!
12 years ago
hello skyler
but plz help am  create table but how can connction to nop commerce  project
and how insert data into table .
plz give me any reference

plz help me
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.