Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Address Add/Edit - remove IFormCollection
support0e20b6228023
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.csindex b005a8337..7ab6771f2 100644--- a/src/Web/Grand.Web/Controllers/AccountController.cs+++ b/src/Web/Grand.Web/Controllers/AccountController.cs@@ -771,7 +771,7 @@ namespace Grand.Web.Controllers[HttpPost][AutoValidateAntiforgeryToken]- public virtual async Task<IActionResult> AddressAdd(CustomerAddressEditModel model, IFormCollection form,+ public virtual async Task<IActionResult> AddressAdd(CustomerAddressEditModel model,[FromServices] AddressSettings addressSettings,[FromServices] IAddressAttributeParser addressAttributeParser){@@ -781,7 +781,7 @@ namespace Grand.Web.Controllersvar customer = _workContext.CurrentCustomer;//custom address attributes- var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes() { Form = form });+ var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes() { SelectedAttributes = model.Address.SelectedAttributes });var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);foreach (var error in customAttributeWarnings){@@ -843,7 +843,7 @@ namespace Grand.Web.Controllers[HttpPost][AutoValidateAntiforgeryToken]- public virtual async Task<IActionResult> AddressEdit(CustomerAddressEditModel model, string addressId, IFormCollection form,+ public virtual async Task<IActionResult> AddressEdit(CustomerAddressEditModel model, string addressId,[FromServices] AddressSettings addressSettings,[FromServices] IAddressAttributeParser addressAttributeParser){@@ -858,14 +858,14 @@ namespace Grand.Web.Controllersreturn RedirectToRoute("CustomerAddresses");//custom address attributes- var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes() { Form = form });+ var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes() { SelectedAttributes = model.Address.SelectedAttributes });var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);foreach (var error in customAttributeWarnings){ModelState.AddModelError("", error);}- if (ModelState.IsValid && ModelState.ErrorCount == 0)+ if (ModelState is { IsValid: true, ErrorCount: 0 }){address = model.Address.ToEntity(address, _workContext.CurrentCustomer, addressSettings);address.Attributes = customAttributes;@@ -887,7 +887,8 @@ namespace Grand.Web.ControllersAddress = address,ExcludeProperties = true,Customer = _workContext.CurrentCustomer,- LoadCountries = () => countries+ LoadCountries = () => countries,+ OverrideAttributes = customAttributes});return View(model);diff --git a/src/Web/Grand.Web/Controllers/CheckoutController.cs b/src/Web/Grand.Web/Controllers/CheckoutController.csindex 3e9fdf72a..87cfa778d 100644--- a/src/Web/Grand.Web/Controllers/CheckoutController.cs+++ b/src/Web/Grand.Web/Controllers/CheckoutController.cs@@ -488,8 +488,7 @@ namespace Grand.Web.Controllerspublic virtual async Task<IActionResult> SaveBilling([FromServices] AddressSettings addressSettings,- CheckoutBillingAddressModel model,- IFormCollection form)+ CheckoutBillingAddressModel model){try{@@ -510,7 +509,7 @@ namespace Grand.Web.Controllerselse{//custom address attributes- var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes() { Form = form });+ var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes() { SelectedAttributes = model.BillingNewAddress.SelectedAttributes });var customAttributeWarnings = await _addressAttributeParser.GetAttributeWarnings(customAttributes);foreach (var error in customAttributeWarnings){@@ -597,8 +596,7 @@ namespace Grand.Web.Controllerspublic virtual async Task<IActionResult> SaveShipping([FromServices] AddressSettings addressSettings,- CheckoutShippingAddressModel model,- IFormCollection form)+ CheckoutShippingAddressModel model){try{@@ -678,7 +676,7 @@ namespace Grand.Web.Controllers{//custom address attributesvar customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes()- { Form = form });+ { SelectedAttributes = model.ShippingNewAddress.SelectedAttributes });var customAttributeWarnings =await _addressAttributeParser.GetAttributeWarnings(customAttributes);foreach (var error in customAttributeWarnings)diff --git a/src/Web/Grand.Web/Controllers/MerchandiseReturnController.cs b/src/Web/Grand.Web/Controllers/MerchandiseReturnController.csindex 826cb58f7..aec553c0f 100644--- a/src/Web/Grand.Web/Controllers/MerchandiseReturnController.cs+++ b/src/Web/Grand.Web/Controllers/MerchandiseReturnController.cs@@ -78,29 +78,27 @@ namespace Grand.Web.Controllers});}- protected async Task<Address> PrepareAddress(MerchandiseReturnModel model, IFormCollection form)+ protected async Task<Address> PrepareAddress(MerchandiseReturnModel model){var address = new Address();- if (_orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)+ if (!_orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress) return address;+ if (!string.IsNullOrEmpty(model.PickupAddressId)){- if (!string.IsNullOrEmpty(model.PickupAddressId))- {- address = _workContext.CurrentCustomer.Addresses.FirstOrDefault(a => a.Id == model.PickupAddressId);- }- else+ address = _workContext.CurrentCustomer.Addresses.FirstOrDefault(a => a.Id == model.PickupAddressId);+ }+ else+ {+ var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes() { SelectedAttributes = model.MerchandiseReturnNewAddress.SelectedAttributes });+ var addressAttributeParser = HttpContext.RequestServices.GetRequiredService<IAddressAttributeParser>();+ var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);+ foreach (var error in customAttributeWarnings){- var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes() { Form = form });- var addressAttributeParser = HttpContext.RequestServices.GetRequiredService<IAddressAttributeParser>();- var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);- foreach (var error in customAttributeWarnings)- {- ModelState.AddModelError("", error);- }- address = model.MerchandiseReturnNewAddress.ToEntity(_workContext.CurrentCustomer, _addressSettings);- model.NewAddressPreselected = true;- address.Attributes = customAttributes;- address.CreatedOnUtc = DateTime.UtcNow;+ ModelState.AddModelError("", error);}+ address = model.MerchandiseReturnNewAddress.ToEntity(_workContext.CurrentCustomer, _addressSettings);+ model.NewAddressPreselected = true;+ address.Attributes = customAttributes;+ address.CreatedOnUtc = DateTime.UtcNow;}return address;}@@ -160,26 +158,26 @@ namespace Grand.Web.ControllersModelState.AddModelError("", _translationService.GetResource("MerchandiseReturns.PickupDateRequired"));}- var address = await PrepareAddress(model, form);+ var address = await PrepareAddress(model);if (ModelState is { IsValid: false }){- var returnmodel = await _mediator.Send(new GetMerchandiseReturn()+ var returnModel = await _mediator.Send(new GetMerchandiseReturn(){Order = order,Language = _workContext.WorkingLanguage,Store = _workContext.CurrentStore});- returnmodel.Error = string.Join(", ", ModelState.Keys.SelectMany(k => ModelState[k]!.Errors).Select(m => m.ErrorMessage).ToArray());- returnmodel.Comments = model.Comments;- returnmodel.PickupDate = model.PickupDate;- returnmodel.NewAddressPreselected = model.NewAddressPreselected;- returnmodel.MerchandiseReturnNewAddress = model.MerchandiseReturnNewAddress;- if (returnmodel.NewAddressPreselected || _orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)+ returnModel.Error = string.Join(", ", ModelState.Keys.SelectMany(k => ModelState[k]!.Errors).Select(m => m.ErrorMessage).ToArray());+ returnModel.Comments = model.Comments;+ returnModel.PickupDate = model.PickupDate;+ returnModel.NewAddressPreselected = model.NewAddressPreselected;+ returnModel.MerchandiseReturnNewAddress = model.MerchandiseReturnNewAddress;+ if (returnModel.NewAddressPreselected || _orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress){await PrepareModelAddress(model.MerchandiseReturnNewAddress, address);}- return View(returnmodel);+ return View(returnModel);}else{@@ -192,24 +190,23 @@ namespace Grand.Web.Controllersreturn View(result.model);}- var returnmodel = await _mediator.Send(new GetMerchandiseReturn()+ var returnModel = await _mediator.Send(new GetMerchandiseReturn(){Order = order,Language = _workContext.WorkingLanguage,Store = _workContext.CurrentStore});- returnmodel.Error = result.model.Error;- returnmodel.Comments = model.Comments;- returnmodel.PickupDate = model.PickupDate;- returnmodel.NewAddressPreselected = model.NewAddressPreselected;- returnmodel.MerchandiseReturnNewAddress = model.MerchandiseReturnNewAddress;- if (returnmodel.NewAddressPreselected || _orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)+ returnModel.Error = result.model.Error;+ returnModel.Comments = model.Comments;+ returnModel.PickupDate = model.PickupDate;+ returnModel.NewAddressPreselected = model.NewAddressPreselected;+ returnModel.MerchandiseReturnNewAddress = model.MerchandiseReturnNewAddress;+ if (returnModel.NewAddressPreselected || _orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress){await PrepareModelAddress(model.MerchandiseReturnNewAddress, address);}- return View(returnmodel);+ return View(returnModel);}-}public virtual async Task<IActionResult> MerchandiseReturnDetails(string merchandiseReturnId)diff --git a/src/Web/Grand.Web/Features/Handlers/Common/GetParseCustomAddressAttributesHandler.cs b/src/Web/Grand.Web/Features/Handlers/Common/GetParseCustomAddressAttributesHandler.csindex b5fcb9186..ea60ec4f3 100644--- a/src/Web/Grand.Web/Features/Handlers/Common/GetParseCustomAddressAttributesHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Common/GetParseCustomAddressAttributesHandler.cs@@ -21,20 +21,19 @@ namespace Grand.Web.Features.Handlers.Commonpublic async Task<IList<CustomAttribute>> Handle(GetParseCustomAddressAttributes request, CancellationToken cancellationToken){- if (request.Form == null)- throw new ArgumentNullException(nameof(request.Form));+ if (request.SelectedAttributes == null)+ throw new ArgumentNullException(nameof(request.SelectedAttributes));var customAttributes = new List<CustomAttribute>();var attributes = await _addressAttributeService.GetAllAddressAttributes();foreach (var attribute in attributes){- string controlId = string.Format("address_attribute_{0}", attribute.Id);switch (attribute.AttributeControlType){case AttributeControlType.DropdownList:case AttributeControlType.RadioList:{- request.Form.TryGetValue(controlId, out var ctrlAttributes);+ var ctrlAttributes = request.SelectedAttributes.FirstOrDefault(x => x.Key == attribute.Id)?.Value;if (!string.IsNullOrEmpty(ctrlAttributes)){customAttributes = _addressAttributeParser.AddAddressAttribute(customAttributes,@@ -44,16 +43,17 @@ namespace Grand.Web.Features.Handlers.Commonbreak;case AttributeControlType.Checkboxes:{- request.Form.TryGetValue(controlId, out var cblAttributes);- if (!String.IsNullOrEmpty(cblAttributes))+ var cblAttributes = request.SelectedAttributes.FirstOrDefault(x => x.Key == attribute.Id)?.Value;+ if (!string.IsNullOrEmpty(cblAttributes)){- foreach (var item in cblAttributes)+ foreach (var item in cblAttributes.Split(',')){- if (!string.IsNullOrEmpty(item))+ if (!String.IsNullOrEmpty(item))customAttributes = _addressAttributeParser.AddAddressAttribute(customAttributes,attribute, item).ToList();}}+}break;case AttributeControlType.ReadonlyCheckboxes:@@ -73,10 +73,10 @@ namespace Grand.Web.Features.Handlers.Commoncase AttributeControlType.TextBox:case AttributeControlType.MultilineTextbox:{- request.Form.TryGetValue(controlId, out var ctrlAttributes);+ var ctrlAttributes = request.SelectedAttributes.FirstOrDefault(x => x.Key == attribute.Id)?.Value;if (!string.IsNullOrEmpty(ctrlAttributes)){- string enteredText = ctrlAttributes.ToString().Trim();+ var enteredText = ctrlAttributes.ToString().Trim();customAttributes = _addressAttributeParser.AddAddressAttribute(customAttributes,attribute, enteredText).ToList();}diff --git a/src/Web/Grand.Web/Features/Models/Common/GetParseCustomAddressAttributes.cs b/src/Web/Grand.Web/Features/Models/Common/GetParseCustomAddressAttributes.csindex b8690dfce..cd6e4a997 100644--- a/src/Web/Grand.Web/Features/Models/Common/GetParseCustomAddressAttributes.cs+++ b/src/Web/Grand.Web/Features/Models/Common/GetParseCustomAddressAttributes.cs@@ -1,11 +1,11 @@using Grand.Domain.Common;+using Grand.Web.Common.Models;using MediatR;-using Microsoft.AspNetCore.Http;namespace Grand.Web.Features.Models.Common{public class GetParseCustomAddressAttributes : IRequest<IList<CustomAttribute>>{- public IFormCollection Form { get; set; }+ public IList<CustomAttributeModel> SelectedAttributes { get; set; }}}diff --git a/src/Web/Grand.Web/Models/Common/AddressModel.cs b/src/Web/Grand.Web/Models/Common/AddressModel.csindex 80cd90f22..3de2f5330 100644--- a/src/Web/Grand.Web/Models/Common/AddressModel.cs+++ b/src/Web/Grand.Web/Models/Common/AddressModel.cs@@ -1,5 +1,8 @@using Grand.Infrastructure.ModelBinding;using Grand.Infrastructure.Models;+using Grand.Web.Common.Binders;+using Grand.Web.Common.Models;+using Microsoft.AspNetCore.Mvc;using Microsoft.AspNetCore.Mvc.Rendering;using System.ComponentModel.DataAnnotations;@@ -93,6 +96,9 @@ namespace Grand.Web.Models.Commonpublic string FormattedCustomAddressAttributes { get; set; }public IList<AddressAttributeModel> CustomAddressAttributes { get; set; }+ [ModelBinder(BinderType = typeof(CustomAttributesBinder))]+ public IList<CustomAttributeModel> SelectedAttributes { get; set; }+public bool AddressTypeEnabled { get; set; }[GrandResourceDisplayName("Address.Fields.AddressType")]diff --git a/src/Web/Grand.Web/Views/Shared/Partials/AddressAttributes.cshtml b/src/Web/Grand.Web/Views/Shared/Partials/AddressAttributes.cshtmlindex bd09e622a..7e88229aa 100644--- a/src/Web/Grand.Web/Views/Shared/Partials/AddressAttributes.cshtml+++ b/src/Web/Grand.Web/Views/Shared/Partials/AddressAttributes.cshtml@@ -4,8 +4,9 @@}@foreach (var attribute in Model){- string controlId = string.Format("address_attribute_{0}", attribute.Id);- string textPrompt = attribute.Name;+ var controlId = $"address_attribute_{attribute.Id}";+ var controlName = $"attributes[{attribute.Id}]";+ var textPrompt = attribute.Name;<div class="inputs custom-attributes col-sm-12 mb-3"><label class="col-form-label" for="@(controlId)">@textPrompt:</label>@@ -19,8 +20,8 @@case AttributeControlType.DropdownList:{- <validation-provider @if (attribute.IsRequired) { <text> rules="required" </text> } name="@(controlId)" v-slot="{ errors, classes }">- <select v-model="createorupdateaddress@{@prefix}.@(controlId)" data-val-required="@(string.Format(Loc["Fields.IsRequired"], textPrompt))" v-bind:class="[classes , 'custom-select form-control']" name="@(controlId)" id="@(controlId)">+ <validation-provider @if (attribute.IsRequired) { <text> rules="required" </text> } v-slot="{ errors, classes }">+ <select v-model="createorupdateaddress@{@prefix}.@(controlId)" data-val-required="@(string.Format(Loc["Fields.IsRequired"], textPrompt))" v-bind:class="[classes , 'custom-select form-control']" name="@(controlName)" id="@(controlId)">@if (!attribute.IsRequired){<option value="">---</option>@@ -57,7 +58,7 @@@foreach (var attributeValue in attribute.Values){<label class="custom-control custom-radio">- <input class="custom-control-input" type="radio" id="@(controlId)_@(attributeValue.Id)" name="@(controlId)" value="@(attributeValue.Id)" v-model="createorupdateaddress@{@prefix}.@(controlId)" @if (attributeValue.IsPreSelected) { <text> data-checked="true" </text> }>+ <input class="custom-control-input" type="radio" id="@(controlId)_@(attributeValue.Id)" name="@(controlName)" value="@(attributeValue.Id)" v-model="createorupdateaddress@{@prefix}.@(controlId)" @if (attributeValue.IsPreSelected) { <text> data-checked="true" </text> }><span class="custom-control-label"></span><span class="custom-control-description">@attributeValue.Name</span></label>@@ -85,7 +86,7 @@@foreach (var attributeValue in attribute.Values){<label class="custom-control custom-checkbox">- <input class="custom-control-input" type="checkbox" id="@(controlId)_@(attributeValue.Id)" name="@(controlId)" value="@(attributeValue.Id)" v-model="createorupdateaddress@{@prefix}.@(controlId)" @(attribute.AttributeControlType == AttributeControlType.ReadonlyCheckboxes ? Html.Raw(" disabled=\"disabled\"") : null)>+ <input class="custom-control-input" type="checkbox" id="@(controlId)_@(attributeValue.Id)" name="@(controlName)" value="@(attributeValue.Id)" v-model="createorupdateaddress@{@prefix}.@(controlId)" @(attribute.AttributeControlType == AttributeControlType.ReadonlyCheckboxes ? Html.Raw(" disabled=\"disabled\"") : null)><span class="custom-control-label"></span><span class="custom-control-description">@attributeValue.Name</span></label>@@ -98,8 +99,8 @@break;case AttributeControlType.TextBox:{- <validation-provider @if (attribute.IsRequired) { <text> rules="required" </text> } name="@(controlId)" v-slot="{ errors, classes }">- <input name="@(controlId)" type="text"+ <validation-provider @if (attribute.IsRequired) { <text> rules="required" </text> } v-slot="{ errors, classes }">+ <input name="@(controlName)" type="text"id="@(controlId)" v-model="createorupdateaddress@{@prefix}.@(controlId)" data-val-required="@(string.Format(Loc["Fields.IsRequired"], textPrompt))" v-bind:class="[classes , 'form-control textbox']" /><span class="field-validation-error">{{ errors[0] }}</span></validation-provider>@@ -107,8 +108,8 @@break;case AttributeControlType.MultilineTextbox:{- <validation-provider @if (attribute.IsRequired) { <text> rules="required" </text> } name="@(controlId)" v-slot="{ errors, classes }">- <textarea name="@(controlId)"+ <validation-provider @if (attribute.IsRequired) { <text> rules="required" </text> } v-slot="{ errors, classes }">+ <textarea name="@(controlName)"id="@(controlId)" v-model="createorupdateaddress@{@prefix}.@(controlId)" data-val-required="@(string.Format(Loc["Fields.IsRequired"], textPrompt))" v-bind:class="[classes , 'form-control']"></textarea><span class="field-validation-error">{{ errors[0] }}</span></validation-provider>