I am added checkbox for filtering "MarkAsNewProduct" in product category page of nopcommerce4.2.
let see the image first,
In this image I am filtering three thing on it. i.e. filtering vendor => category => markasnewproduct
Now vendor and category filter is happening perfectly while when I check the checkbox for the filter of markasnewproduct at that time filter is not working as per the new product.
Here is my controller code,
[HttpsRequirement(SslRequirement.No)]
public virtual IActionResult Vendor(int vendorId, CatalogPagingFilteringModel command, int categoryId, bool markAsNewProduct)
{
//int abc = Convert.ToInt32(ViewBag.CategoryId);
var vendor = _vendorService.GetVendorById(vendorId);
if (vendor == null || vendor.Deleted || !vendor.Active)
return InvokeHttp404();
//'Continue shopping' URL
_genericAttributeService.SaveAttribute(_workContext.CurrentCustomer,
NopCustomerDefaults.LastContinueShoppingPageAttribute,
_webHelper.GetThisPageUrl(false),
_storeContext.CurrentStore.Id);
//display "edit" (manage) link
if (_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageVendors))
DisplayEditLink(Url.Action("Edit", "Vendor", new { id = vendor.Id, area = AreaNames.Admin }));
//model
var model = _catalogModelFactory.PrepareVendorModel(vendor, command, categoryId, markAsNewProduct);
return View(model);
}
Here is the view,
@model VendorModel
@using Nop.Core.Domain.Seo
@inject Nop.Core.IWebHelper webHelper
@inject SeoSettings seoSettings
@{
Layout = "_ColumnsTwo";
//title
Html.AddTitleParts(!string.IsNullOrEmpty(Model.MetaTitle) ? Model.MetaTitle : Model.Name);
//meta
Html.AddMetaDescriptionParts(Model.MetaDescription);
Html.AddMetaKeywordParts(Model.MetaKeywords);
//page class
Html.AppendPageCssClassParts("html-vendor-page");
if (seoSettings.CanonicalUrlsEnabled)
{
var vendorUrl = Url.RouteUrl("Vendor", new { SeName = Model.SeName }, webHelper.CurrentRequestProtocol).ToLowerInvariant();
Html.AddCanonicalUrlParts(vendorUrl, seoSettings.QueryStringInCanonicalUrlsEnabled);
}
}
<form asp-route="Vendor" method="post">
<input type="hidden" value="@Model.CategoryId" id="CategoryId" name="categoryId" />
<input type="submit" value="SUBMIT" id="submit" style="display: none;" />
<div class="page vendor-page">
<div class="page-title">
<h1>@Model.Name</h1>
</div>
<div class="page-body">
@await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.VendorDetailsTop, additionalData = Model })
<div class="contact-vendor">
<input type="button" value="@T("ContactVendor")" class="button-2 contact-vendor-button"
onclick="setLocation('@Url.RouteUrl("Vendordetail", new { vendorId = Model.Id })')" />
<input asp-for="markAsNewProduct" onclick="MarkAsNewProductCheck()" /> MarkAsNewProduct
</div>
@if (Model.Products.Count > 0)
{
@await Html.PartialAsync("_CatalogSelectors", Model.PagingFilteringContext)
}
@*product list*@
@if (Model.Products.Count > 0)
{
<div class="@(Model.PagingFilteringContext.ViewMode == "list" ? "product-list" : "product-grid")">
<div class="item-grid">
@foreach (var product in Model.Products)
{
<div class="item-box">
@await Html.PartialAsync("_ProductBox", product)
</div>
}
</div>
</div>
}
@{
var pager = Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber");
}
@if (!pager.IsEmpty())
{
<div class="pager">
@pager
</div>
}
@await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.VendorDetailsBottom, additionalData = Model })
</div>
</div>
</form>
<script asp-location="Footer">
function MarkAsNewProductCheck() {
$('#submit').trigger('click');
}
</script>
Now, in this code controller code i.e.
public virtual IActionResult Vendor(int vendorId, CatalogPagingFilteringModel command, int categoryId, bool markAsNewProduct)
in the parameter markasnewproduct value is right coming. For ex: if I uncheck the checkbox then it shows the false value while when I check the checkbox it shows me the true value.