Skip to content

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.

Checkout attributes - remove IFormCollection

supporteb4144ff8f59
diff --git a/src/Web/Grand.Web/Commands/Handler/ShoppingCart/SaveCheckoutAttributesCommandHandler.cs b/src/Web/Grand.Web/Commands/Handler/ShoppingCart/SaveCheckoutAttributesCommandHandler.cs
index 6e7548a04..466de754f 100644
--- a/src/Web/Grand.Web/Commands/Handler/ShoppingCart/SaveCheckoutAttributesCommandHandler.cs
+++ b/src/Web/Grand.Web/Commands/Handler/ShoppingCart/SaveCheckoutAttributesCommandHandler.cs
@@ -34,14 +34,13 @@ namespace Grand.Web.Commands.Handler.ShoppingCart
if (request.Cart == null)
throw new ArgumentNullException(nameof(request.Cart));
- 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 checkoutAttributes = await _checkoutAttributeService.GetAllCheckoutAttributes(request.Store.Id, !request.Cart.RequiresShipping());
foreach (var attribute in checkoutAttributes)
{
- string controlId = string.Format("checkout_attribute_{0}", attribute.Id);
switch (attribute.AttributeControlTypeId)
{
case AttributeControlType.DropdownList:
@@ -49,7 +48,7 @@ namespace Grand.Web.Commands.Handler.ShoppingCart
case AttributeControlType.ColorSquares:
case AttributeControlType.ImageSquares:
{
- request.Form.TryGetValue(controlId, out var ctrlAttributes);
+ var ctrlAttributes = request.SelectedAttributes.FirstOrDefault(x => x.Key == attribute.Id)?.Value;
if (!string.IsNullOrEmpty(ctrlAttributes))
{
customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes,
@@ -60,14 +59,17 @@ namespace Grand.Web.Commands.Handler.ShoppingCart
break;
case AttributeControlType.Checkboxes:
{
- request.Form.TryGetValue(controlId, out var 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(','))
{
- customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes, attribute, item).ToList();
+ if (!string.IsNullOrEmpty(item))
+ customAttributes = _checkoutAttributeParser.AddCheckoutAttribute(customAttributes,
+ attribute, item).ToList();
}
}
+
}
break;
case AttributeControlType.ReadonlyCheckboxes:
@@ -88,7 +90,7 @@ namespace Grand.Web.Commands.Handler.ShoppingCart
case AttributeControlType.MultilineTextbox:
case AttributeControlType.Datepicker:
{
- 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();
@@ -99,10 +101,8 @@ namespace Grand.Web.Commands.Handler.ShoppingCart
break;
case AttributeControlType.FileUpload:
{
- Guid downloadGuid;
- request.Form.TryGetValue(controlId, out var guid);
-
- Guid.TryParse(guid, out downloadGuid);
+ var guid = request.SelectedAttributes.FirstOrDefault(x => x.Key == attribute.Id)?.Value;
+ Guid.TryParse(guid, out var downloadGuid);
var download = await _downloadService.GetDownloadByGuid(downloadGuid);
if (download != null)
{
diff --git a/src/Web/Grand.Web/Commands/Models/ShoppingCart/SaveCheckoutAttributesCommand.cs b/src/Web/Grand.Web/Commands/Models/ShoppingCart/SaveCheckoutAttributesCommand.cs
index cf976e334..db90c95a4 100644
--- a/src/Web/Grand.Web/Commands/Models/ShoppingCart/SaveCheckoutAttributesCommand.cs
+++ b/src/Web/Grand.Web/Commands/Models/ShoppingCart/SaveCheckoutAttributesCommand.cs
@@ -2,8 +2,8 @@
using Grand.Domain.Customers;
using Grand.Domain.Orders;
using Grand.Domain.Stores;
+using Grand.Web.Common.Models;
using MediatR;
-using Microsoft.AspNetCore.Http;
namespace Grand.Web.Commands.Models.ShoppingCart
{
@@ -13,6 +13,6 @@ namespace Grand.Web.Commands.Models.ShoppingCart
public Store Store { get; set; }
public IList<ShoppingCartItem> Cart { get; set; }
- public IFormCollection Form { get; set; }
+ public IList<CustomAttributeModel> SelectedAttributes { get; set; }
}
}
diff --git a/src/Web/Grand.Web/Controllers/ShoppingCartController.cs b/src/Web/Grand.Web/Controllers/ShoppingCartController.cs
index 2cec43a9a..438237db8 100644
--- a/src/Web/Grand.Web/Controllers/ShoppingCartController.cs
+++ b/src/Web/Grand.Web/Controllers/ShoppingCartController.cs
@@ -22,6 +22,7 @@ using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Grand.Web.Common.Extensions;
+using Grand.Web.Models.ShoppingCart;
namespace Grand.Web.Controllers
{
@@ -116,7 +117,7 @@ namespace Grand.Web.Controllers
[DenySystemAccount]
[HttpPost]
- public virtual async Task<IActionResult> CheckoutAttributeChange(IFormCollection form,
+ public virtual async Task<IActionResult> CheckoutAttributeChange(CheckoutAttributeSelectedModel model,
[FromServices] ICheckoutAttributeParser checkoutAttributeParser,
[FromServices] ICheckoutAttributeFormatter checkoutAttributeFormatter)
{
@@ -126,7 +127,7 @@ namespace Grand.Web.Controllers
Customer = _workContext.CurrentCustomer,
Store = _workContext.CurrentStore,
Cart = cart,
- Form = form
+ SelectedAttributes = model.Attributes
});
var enabledAttributeIds = new List<string>();
@@ -143,7 +144,7 @@ namespace Grand.Web.Controllers
disabledAttributeIds.Add(attribute.Id);
}
}
- var model = await _mediator.Send(new GetOrderTotals() {
+ var orderTotals = await _mediator.Send(new GetOrderTotals() {
Cart = cart,
IsEditable = true,
Store = _workContext.CurrentStore,
@@ -157,7 +158,7 @@ namespace Grand.Web.Controllers
{
enabledattributeids = enabledAttributeIds.ToArray(),
disabledattributeids = disabledAttributeIds.ToArray(),
- model,
+ model = orderTotals,
checkoutattributeinfo = await checkoutAttributeFormatter.FormatAttributes(checkoutAttributes, _workContext.CurrentCustomer),
});
}
@@ -502,18 +503,18 @@ namespace Grand.Web.Controllers
}
[DenySystemAccount]
- public virtual async Task<IActionResult> StartCheckout(IFormCollection form = null)
+ public virtual async Task<IActionResult> StartCheckout(CheckoutAttributeSelectedModel model)
{
var cart = await _shoppingCartService.GetShoppingCart(_workContext.CurrentStore.Id, ShoppingCartType.ShoppingCart, ShoppingCartType.Auctions);
- var checkoutAttributes = new List<CustomAttribute>();
+ List<CustomAttribute> checkoutAttributes;
//parse and save checkout attributes
- if (form != null && form.Count > 0)
+ if (model?.Attributes != null && model.Attributes.Count > 0)
{
checkoutAttributes = (await _mediator.Send(new SaveCheckoutAttributesCommand() {
Customer = _workContext.CurrentCustomer,
Store = _workContext.CurrentStore,
Cart = cart,
- Form = form
+ SelectedAttributes = model.Attributes
})).ToList();
}
else
@@ -528,15 +529,12 @@ namespace Grand.Web.Controllers
}
//everything is OK
- if (await _groupService.IsGuest(_workContext.CurrentCustomer))
- {
- if (!_orderSettings.AnonymousCheckoutAllowed)
- return Challenge();
+ if (!await _groupService.IsGuest(_workContext.CurrentCustomer)) return RedirectToRoute("Checkout");
+ if (!_orderSettings.AnonymousCheckoutAllowed)
+ return Challenge();
- return RedirectToRoute("LoginCheckoutAsGuest", new { returnUrl = Url.RouteUrl("ShoppingCart") });
- }
+ return RedirectToRoute("LoginCheckoutAsGuest", new { returnUrl = Url.RouteUrl("ShoppingCart") });
- return RedirectToRoute("Checkout");
}
[AutoValidateAntiforgeryToken]
diff --git a/src/Web/Grand.Web/Models/ShoppingCart/CheckoutAttributeSelectedModel.cs b/src/Web/Grand.Web/Models/ShoppingCart/CheckoutAttributeSelectedModel.cs
new file mode 100644
index 000000000..27b1ab02a
--- /dev/null
+++ b/src/Web/Grand.Web/Models/ShoppingCart/CheckoutAttributeSelectedModel.cs
@@ -0,0 +1,15 @@
+using Grand.Web.Common.Binders;
+using Grand.Web.Common.Models;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Grand.Web.Models.ShoppingCart;
+
+public class CheckoutAttributeSelectedModel
+{
+ public CheckoutAttributeSelectedModel()
+ {
+ Attributes = new List<CustomAttributeModel>();
+ }
+ [ModelBinder(BinderType = typeof(CustomAttributesBinder))]
+ public IList<CustomAttributeModel> Attributes { get; set; }
+}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Views/ShoppingCart/Partials/CheckoutAttributes.cshtml b/src/Web/Grand.Web/Views/ShoppingCart/Partials/CheckoutAttributes.cshtml
index e2a4a30f5..8255f29ab 100644
--- a/src/Web/Grand.Web/Views/ShoppingCart/Partials/CheckoutAttributes.cshtml
+++ b/src/Web/Grand.Web/Views/ShoppingCart/Partials/CheckoutAttributes.cshtml
@@ -11,11 +11,11 @@
<span v-if="attribute.IsRequired" class="required">*</span>
<template v-if="attribute.AttributeControlType == 4">
<label :for="'checkout_attribute_' + attribute.Id" class="sr-only">checkout_attribute_{{attribute.Id}}</label>
- <input :name="'checkout_attribute_' + attribute.Id" type="text" class="form-control textbox" :id="'checkout_attribute_' + attribute.Id" v-model="attribute.DefaultValue" :value="attribute.DefaultValue" onchange="checkoutAttributeChange();" />
+ <input :name="'attributes['+attribute.Id+']'" type="text" class="form-control textbox" :id="'checkout_attribute_' + attribute.Id" v-model="attribute.DefaultValue" :value="attribute.DefaultValue" onchange="checkoutAttributeChange();" />
</template>
<template v-if="attribute.AttributeControlType == 1">
<label :for="'checkout_attribute_' + attribute.Id" class="sr-only">checkout_attribute_{{attribute.Id}}</label>
- <select :id="'checkout_attribute_' + attribute.Id" class="form-control custom-select" onchange="checkoutAttributeChange()" :name="'checkout_attribute_' + attribute.Id">
+ <select :id="'checkout_attribute_' + attribute.Id" class="form-control custom-select" onchange="checkoutAttributeChange()" :name="'attributes['+attribute.Id+']'">
<template v-if="attribute.IsRequired == false">
<option value="">---</option>
</template>
@@ -37,7 +37,7 @@
checkout_attribute_{{attribute.Id}}
</legend>
<label class="custom-control custom-radio">
- <input class="custom-control-input" :id="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" type="radio" :name="'checkout_attribute_' + attribute.Id" :value="attributeValue.Id" :checked="attributeValue.IsPreSelected" onclick="checkoutAttributeChange()" />
+ <input class="custom-control-input" :id="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" type="radio" :name="'attributes['+attribute.Id+']'" :value="attributeValue.Id" :checked="attributeValue.IsPreSelected" onclick="checkoutAttributeChange()" />
<span class="custom-control-label"></span>
<span class="custom-control-description" :for="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id">
<template v-if="attributeValue.PriceAdjustment == null">
@@ -59,8 +59,8 @@
<ul class="option-list px-0 mb-0">
<li v-for="attributeValue in attribute.Values">
<label class="custom-control custom-checkbox">
- <input v-if="attribute.AttributeControlType == 50" class="custom-control-input" :id="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" type="checkbox" :name="'checkout_attribute_' + attribute.Id" :value="attributeValue.Id" onclick="checkoutAttributeChange()" :checked="attributeValue.IsPreSelected" disabled="disabled" />
- <input v-else class="custom-control-input" :id="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" type="checkbox" :name="'checkout_attribute_' + attribute.Id" :value="attributeValue.Id" onclick="checkoutAttributeChange()" :checked="attributeValue.IsPreSelected" />
+ <input v-if="attribute.AttributeControlType == 50" class="custom-control-input" :id="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" type="checkbox" :name="'attributes['+attribute.Id+']'" :value="attributeValue.Id" onclick="checkoutAttributeChange()" :checked="attributeValue.IsPreSelected" disabled="disabled" />
+ <input v-else class="custom-control-input" :id="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" type="checkbox" :name="'attributes['+attribute.Id+']'" :value="attributeValue.Id" onclick="checkoutAttributeChange()" :checked="attributeValue.IsPreSelected" />
<span class="custom-control-label"></span>
<span class="custom-control-description" :for="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id">
<template v-if="attributeValue.PriceAdjustment == null">
@@ -76,10 +76,10 @@
</fieldset>
</template>
<template v-if="attribute.AttributeControlType == 10">
- <textarea class="form-control" :id="'checkout_attribute_' + attribute.Id" :name="'checkout_attribute_' + attribute.Id" v-model="attribute.DefaultValue" :value="attribute.DefaultValue" onchange="checkoutAttributeChange()">{{attribute.DefaultValue}}</textarea>
+ <textarea class="form-control" :id="'checkout_attribute_' + attribute.Id" :name="'attributes['+attribute.Id+']'" v-model="attribute.DefaultValue" :value="attribute.DefaultValue" onchange="checkoutAttributeChange()">{{attribute.DefaultValue}}</textarea>
</template>
<template v-if="attribute.AttributeControlType == 20">
- <input :id="'checkout_attribute_' + attribute.Id" :name="'checkout_attribute_' + attribute.Id" class="form-control" :value="attribute.DefaultValue" v-model="attribute.DefaultValue" onchange="checkoutAttributeChange();" type="date" />
+ <input :id="'checkout_attribute_' + attribute.Id" :name="'attributes['+attribute.Id+']'" class="form-control" :value="attribute.DefaultValue" v-model="attribute.DefaultValue" onchange="checkoutAttributeChange();" type="date" />
</template>
<template v-if="attribute.AttributeControlType == 30">
<template>
@@ -87,7 +87,7 @@
<b-form-file :accept="attribute.AllowedFileExtensions" type="file" id="qqfile" name="qqfile" :data-url="'/uploadfilecheckoutattribute/' + attribute.Id" :attribute="attribute.Id" onchange="uploadFile(this)" />
</div>
</template>
- <input class="hidden-upload-input" type="hidden" onchange="checkoutAttributeChange()" :id="'checkout_attribute_' + attribute.Id" :name="'checkout_attribute_' + attribute.Id" v-model="attribute.DefaultValue" :value="attribute.DefaultValue" />
+ <input class="hidden-upload-input" type="hidden" onchange="checkoutAttributeChange()" :id="'checkout_attribute_' + attribute.Id" :name="'attributes['+attribute.Id+']'" v-model="attribute.DefaultValue" :value="attribute.DefaultValue" />
<div id="download-message" class="alert my-2" style="display:none;"></div>
<a :id="'checkout_attribute_' + attribute.Id + 'downloadurl'" class="download-file btn btn-outline-info" style="display:none;">
Download
@@ -143,7 +143,7 @@
<ul class="option-list color-squares w-100 d-inline-flex flex-wrap px-0 mb-0" :id="'color-squares-' + attribute.Id">
<li v-for="attributeValue in attribute.Values" class="mr-1 p-0">
<label :for="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" class="mb-0">
- <input :id="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" type="radio" :name="'checkout_attribute_' + attribute.Id" :value="attributeValue.Id" onchange="checkoutAttributeChange()" :checked="attributeValue.IsPreSelected" />
+ <input :id="'checkout_attribute_' + attribute.Id + '_' + attributeValue.Id" type="radio" :name="'attributes['+attribute.Id+']'" :value="attributeValue.Id" onchange="checkoutAttributeChange()" :checked="attributeValue.IsPreSelected" />
<template v-if="attributeValue.PriceAdjustment == null">
<span class="color-container" :title="attributeValue.Name">
<span class="color" :style="'background-color:' + attributeValue.ColorSquaresRgb"></span>