How to add a new column in customer register?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 ano atrás
ora1998 wrote:

                if (_customerSettings.groupownerEnabled)
                    await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);


as we can see that you have stored the value into Generic attribute , so you need to assign the value from the generic attribute only and store from there only ,

you can take reference from the other field of generic attribute click here to see the screenshot you need to assign like this

 model.groupowner= await _genericAttributeService.GetAttributeAsync<string>(customer, NopCustomerDefaults.groupownerAttribute);

For getting the value from generic attribute

for SAVE or SET The value

await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);
1 ano atrás
Thanks for ur reply, after debugging, I still did not fix the problem yet, the reason is, when I change the value in the page "My account - Customer info", I can see the value has been changed to the new value, like 12345(old) to 55555(new).
                    if (_customerSettings.groupownerEnabled)
                        await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);

However, sames like the new data has not been saved into the database after the above code., so when the code skip to the next step, the page will be refreshed, so the data will go back to the old value(12345) after the below code, any idea? thanks.
in PrepareCustomerInfoModelAsync:
                //model.groupowner = await _genericAttributeService.GetAttributeAsync<string>(customer, NopCustomerDefaults.groupownerAttribute);
                model.groupowner = customer.groupowner;

BTW, I cannot use the below code since I have to fetch the data in the 1st time, that's why I switch to (model.groupowner = customer.groupowner;) as you said, fyi.
                //model.groupowner = await _genericAttributeService.GetAttributeAsync<string>(customer, NopCustomerDefaults.groupownerAttribute);

Thanks.
1 ano atrás
did you verify the things in the Database the changes are reflected?

I am a bit confused are you store the value in the DB of the customer Table or the Generic attribute ?
1 ano atrás
did you verify the things in the Database the changes are reflected?
-- Yes I verified in the database, the value did not change, still the old value.

I am a bit confused are you store the value in the DB of the customer Table or the Generic attribute ?
-- I am just using the below code in PrepareCustomerInfoModelAsync to save the data, any other method to save the data?
                //model.groupowner = await _genericAttributeService.GetAttributeAsync<string>(customer, NopCustomerDefaults.groupownerAttribute);
                model.groupowner = customer.groupowner;

my expectation is to save the data in DB, when I change the data(groupowner) in the page of "My account - Customer info", then click the button "Submit", the new data can be saved to the database. Thanks.
1 ano atrás
YES so you are storing data in the Customer table

so in the post method, you just need to assign a model value into customer object
I have already explained the flow before this 2 post inot same forum post

You need to assign model value into customer object
Customer.groupowner= model.groupowner
1 ano atrás
I'd put the code into CustomerController.cs

                    await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);
                    customer.groupowner = model.groupowner;

The result is still same as before(data did not save), after debugging, I found the code will always run into the below except. Then the code will re-fetch the data from the database, so the result is still same as below, why got this?


ForceMultiFactorAuthenticationAttribute
            /// <summary>
            /// Called asynchronously before the action, after model binding is complete.
            /// </summary>
            /// <param name="context">A context for action filters</param>
            /// <param name="next">A delegate invoked to execute the next action filter or the action itself</param>
            /// <returns>A task that represents the asynchronous operation</returns>
            public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
            {
                await ValidateAuthenticationForceAsync(context);
                if (context.Result == null)
                    await next();
            }
1 ano atrás
I am confused what you are trying to do ?
There are two different ways of storing a value for a customer
Do you want to store the value from model.groupowner
in the customer table or as a Generic Attribute

This is storing the value as generic attribute
Customer.groupowner = model.groupowner
model.groupowner= await _genericAttributeService.GetAttributeAsync<string>(customer, NopCustomerDefaults.groupownerAttribute);

Which you do not need to do if you are storing the value in the customer table i.e.
Customer.groupowner = model.groupowner
Then update the record

Maybe if you first explain a bit clearer what you are trying to do without using code it might be helpful
1 ano atrás
This is my code:

                    _customerSettings.groupownerEnabled = true;
                    if (_customerSettings.groupownerEnabled)
                    {
                        await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);
                        customer.groupowner = model.groupowner;

                    }

Via debugging, after this code, I can see:
customer.groupowner changed from '12345' to '55555'
mode.groupowner='55555', which is what I changed from the web page.

My question is, after above code, the data has not been written to the DB.
1 ano atrás
I have verified:
1. the data has NOT been written to the DB, after:
customer.groupowner= model.groupowner
not sure why?

2. Attribute has been changed by the below code, I can see the value has been changed on the page after the button "Save", but the data is not in the DB. How can I save the data to DB? #1 ddid not work.
await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);

Thanks.
1 ano atrás
As per Yidna's question, how are you trying to store the data?  It seems that you are trying to put it in BOTH the Customer table backed field, and ALSO in Generic Attributes.   Why?

await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.groupownerAttribute, model.groupowner);

customer.groupowner = model.groupowner;


(P.S. By standard convention in C#, a Property like "groupowner" should be spelled caps first char and camel case: "GroupOwner"  ;)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.