New contact page, Customer class, _workContext.CurrentCustomer problem

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Hace 11 años
Hello all,

I've been trying to teach myself MVC, and have replicated the Contact Us form calling it Request a Demo.

I have added it to the route, and controller. I have built a new model, validator, and view.

I am using CommonController.cs, in there it has

 //contact us page
        public ActionResult ContactUs()
        {
            var model = new ContactUsModel()
            {
                Email = _workContext.CurrentCustomer.Email,
                FullName = _workContext.CurrentCustomer.GetFullName(),
                DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage
            };
            return View(model);
        }


I replicated this adding to CommonController.cs

//request a demo page
        public ActionResult RequestADemo()
        {
            var model = new RequestADemoModel()
            {
                Title = _workContext.CurrentCustomer.Title,
                FirstName = _workContext.CurrentCustomer.FirstName,
                Surname = _workContext.CurrentCustomer.Surname,
                Company = _workContext.CurrentCustomer.Company,
                Premises = _workContext.CurrentCustomer.Premises,
                Street = _workContext.CurrentCustomer.Street,
                Street2 = _workContext.CurrentCustomer.Street2,
                Town = _workContext.CurrentCustomer.Town,
                County = _workContext.CurrentCustomer.County,
                Postcode = _workContext.CurrentCustomer.Postcode,
                Telephone = _workContext.CurrentCustomer.Telephone,
                DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage
            };
            return View(model);
        }


The problem with this is it uses _workContext.CurrentCustomer, and expects all my fields to be part of the Customer class. That's ok, I can add all those properties. My next problem is if I do that it expects to find all those properties in the Customer table in the database.

Server Error in '/' Application.

Invalid column name 'Title'.
Invalid column name 'Initial'.
Invalid column name 'Surname'.
Invalid column name 'Company'. .......


I think I'm going wrong my using _workContext.CurrentCustomer in the controller. Once I have this working I would like to build a plugin to release to the community. Should I write my own controller instead?

Can anyone advise please?

Thank you
Hace 11 años
Oh, I have kind of figured it out.

I see that _workContext.CurrentCustomer must point to an instance of the Customer class. What I'm doing is accidentally trying to set the values of the form to those stored in the customer database under the logged in account. I just removed all the assignments

        //request a demo page
        public ActionResult RequestADemo()
        {
            var model = new RequestADemoModel()
            {
               DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage
            };
            return View(model);
        }


and that has solved that problem.

I see that the customer object is set in CommonController.cs by being passed in to the class constructor

public CommonController(ICategoryService categoryService, IProductService productService,
            IManufacturerService manufacturerService, ITopicService topicService,
            ILanguageService languageService,
            ICurrencyService currencyService, ILocalizationService localizationService,
            IWorkContext workContext,
            IQueuedEmailService queuedEmailService, IEmailAccountService emailAccountService,
            ISitemapGenerator sitemapGenerator, IThemeContext themeContext,
            IThemeProvider themeProvider, IForumService forumService,
            ICustomerService customerService, IWebHelper webHelper,
            IPermissionService permissionService, IMobileDeviceHelper mobileDeviceHelper,
            HttpContextBase httpContext, CustomerSettings customerSettings,
            TaxSettings taxSettings, CatalogSettings catalogSettings,
            StoreInformationSettings storeInformationSettings, EmailAccountSettings emailAccountSettings,
            CommonSettings commonSettings, BlogSettings blogSettings, ForumSettings forumSettings,
            LocalizationSettings localizationSettings, CaptchaSettings captchaSettings)
        {


Can anyone point out (for my own learning) where this CommonController class in instantiated please?

Thank you
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.