How to add a column for States in the telerik grid for the Order List in the Admin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
I would like to add a column to the order list that would allow me to see what state the customer is from.

I tried to edit the grid as follows:

Opened

Nop.Admin -> Views -> Order -> List.cshtml

added

columns.Bound(x => x.BillingAddress.StateProvinceName);

right after

columns.Bound(x => x.Id);

in the code.  It looks like it should work but I get an error that says

"Microsoft JScript runtime error:  Unable to get value of the property
'StateProvinceName': object is null or undefined"

If someone knows what I am doing wrong that would great.  Thanks in advance!

I am using nopcommerce version 2.65
11 years ago
I tested what you did and found that is happening.  The problem is coming from Nop.Admin.Controllers.OrderController.cs, in the OrderList method (around line 542).  This is the method that loads the grid up for you and returns a Jason Result.

To remedy the situation, here is what I did.

Order Model (Nop.Admin.Models.Orders)

Added a string called StateProvinceName.

View

Used columns.Bound(x => x.StateProvinceName)

Order Controller (Nop.Admin.Controllers)

Here is what my gridModel looks like now.


var gridModel = new GridModel<OrderModel>
            {
                Data = orders.Select(x =>
                {
                    return new OrderModel()
                    {
                        Id = x.Id,
                        OrderTotal = _priceFormatter.FormatPrice(x.OrderTotal, true, false),
                        OrderStatus = x.OrderStatus.GetLocalizedEnum(_localizationService, _workContext),
                        PaymentStatus = x.PaymentStatus.GetLocalizedEnum(_localizationService, _workContext),
                        ShippingStatus = x.ShippingStatus.GetLocalizedEnum(_localizationService, _workContext),
                        CustomerEmail = x.BillingAddress.Email,
                        CreatedOn = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc),
                        StateProvinceName = x.BillingAddress.StateProvince.Name
                    };
                }),
                Total = orders.TotalCount
            };


Hope this helps.  Cheers.
11 years ago
Thanks for the help!  I appreciate you taking the time to find the solution.  

I will have to try this out.  It will be great to be able to implement this; it will keep me from having to view every order to see what state it's from. :-)  Thanks!
11 years ago
chajo10 wrote:
StateProvinceName = x.BillingAddress.StateProvince.Name

Do not forget about null validation
11 years ago
With strings it shouldn't matter.  Most other types would probably throw an error (long story short, DateTime? is a pain in the butt, lol).
11 years ago
x.BillingAddress.StateProvince could be null when a country does not have any states configured
11 years ago
That is true.  My mistake.  Thanks for correction.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.