Overide Model Property of assciated System.Web.MVC.ViewDataDictionary object

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hi

My question - I have Input Button in the Category.Template View.

 <input type="submit" value="@T("Order")" class="productlistaddtocartbutton" onclick="setLocation('@(@Url.RouteUrl("ConcordAddMultipleProductsToCart", new { productId = Model.Id}))/' + $('#quantity_').val())" />


Is there a work around so I can overide System.Web.MVC.ViewDataDictionary object
<CategoryModel>
for Model.Id highlighted in Bold to accept System.Web.MVC.ViewDataDictionary object
<ProductModel>

In the Category.Template... Not foreaching an
IEnumerable<List>


Thank you

Kind Regards

Richard
11 years ago
Please post the entire cshtml file
11 years ago
Two Views CategoryTemplate and ProductBox.CSHTML updated for this companies needs...

CategoryTemplate.ProductInGridOrLines.

@*Richard Evans 23-7-2012
  Description - Updated new Category Template View - Removing the Subcategory's and Category image display as it looks to retail
                Applying nessercary changes to include Html.Actions both the Hompage Text and ConcordNews.*@
@model CategoryModel
@{
    Layout = "~/Views/Shared/_ColumnsThree.cshtml";
    Html.AddTitleParts(!String.IsNullOrEmpty(Model.MetaTitle) ? Model.MetaTitle : Model.Name);
    Html.AddMetaDescriptionParts(Model.MetaDescription);
    Html.AddMetaKeywordParts(Model.MetaKeywords);

    var canonicalUrlsEnabled = EngineContext.Current.Resolve<SeoSettings>().CanonicalUrlsEnabled;
    if (canonicalUrlsEnabled)
    {
        var categoryUrl = Url.RouteUrl("Category", new { categoryId = Model.Id, SeName = Model.SeName }, this.Request.Url.Scheme);
        Html.AddCanonicalUrlParts(categoryUrl);
    }
}
@using Nop.Core;
@using Nop.Core.Domain.Common;
@using Nop.Core.Infrastructure;
@using Nop.Web;
@using Nop.Web.Framework.UI;
@using Nop.Web.Models.Catalog;
@using Nop.Web.Extensions;

@if (Model.Products.Count == 0)
{
    @Html.Action("TopicBlock", "Topic", new { systemName = "HomePageText" })
    <div class="clear">
    </div>
    @Html.Action("HomePageNews", "News")
}

@*product list*@
@if (Model.Products.Count > 0)
{


    if (Model.DisplayCategoryBreadcrumb)
    {
                
         <div class="breadcrumb">
           <a href="@Url.RouteUrl("HomePage")">@T("Home")</a> /
            @for (int i = 0; i < Model.CategoryBreadcrumb.Count; i++)
            {
                var catBr = Model.CategoryBreadcrumb[i];
                <a href="@Url.RouteUrl("Category", new { categoryId = catBr.Id, SeName = catBr.SeName })">
                @catBr.Name</a>
                                                                                                    
           if (i != Model.CategoryBreadcrumb.Count - 1)
           {
                    <text>/</text>
                                                                                                    
           }
    }
            <br />
            </div>
            

<table class="concord-table">
    
     <colgroup>
    
      <col class="picture"     width="1" />
      <col class="stockcode"   width="1" />
      <col class="name"        width="1" />
      <col class="pack"        width="1" />
      <col class="price"       width="1" />
      <col class="rrp"         width="1" />
      <col class="cart"        width="1" />
    
     </colgroup>

    <tr class="concord-header-row">
     <th class="picture">
         @T("Picture")
     </th>
     <th class="stockcode">
         @T("StockCode")
     </th>
     <th class="name">
         @T("Product Name")
     </th>
     <th class="pack">
         @T("Pack Size")
     </th>
     <th class="price">
         @T("WholeSale Pack Price")
     </th>
     <th class="MRP">
         @T("MRP")
     </th>
     <th class="cart">
         @T("QTY")
     </th>
     </tr>
     <tbody>
    
      @foreach (var product in Model.Products)
      {
      <tr>
      @Html.Partial("_ConcordProductBox", product)
      </tr>
      }
    
    
     <tr class = "addtocart">
     <td>
     <input type="submit" value="@T("Order")" class="productlistaddtocartbutton" onclick="setLocation('@(@Url.RouteUrl("ConcordAddMultipleProductsToCart", new { productId = Model.Id }))/' + $('#quantity_').val())" />
     </td>
     </tr>
   </tbody>
    </table>
      
    }

}
<div class="pager">
  @Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber")
</div>


_ProductBox.CSHTML



@*Richard Evans 10/07/2012  -- No point In putting Line Numbers here as its the whole View.
Description = New Product Box for Concord Cash and Carry. Displaying nessercary Data from Stock List
A View Is follow's --
Views are the components that display the application's user interface (UI).
Typically, this UI is created from the model data*@
@model Nop.Web.Models.Catalog.ProductModel
@using Nop.Web.Models.Catalog
<tr class ="concord-product">
   <td class="picture">
   <a href="javascript:OpenWindow('@Url.RouteUrl("Product", new { productId = Model.Id, SeName = Model.SeName, formId = "Product-form" }))', 700, 400, true);" title="@Model.DefaultPictureModel.Title" />
   <img alt="@Model.DefaultPictureModel.AlternateText" src="@Model.DefaultPictureModel.ImageUrl" title="@Model.DefaultPictureModel.Title" />
   </td>
    @*<a class="product-details-page" href="javascript:OpenWindow('@Url.RouteUrl("Product", new { productId = Model.Id, SeName = Model.SeName, formId = "Product-form" }))', 700, 400, true);" title="@Model.DefaultPictureModel.Title">
    <img alt="@Model.DefaultPictureModel.AlternateText" src="@Model.DefaultPictureModel.ImageUrl" title="@Model.DefaultPictureModel.Title" />
    </a>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".product-details-page").fancybox();
        });
    </script>*@
   <td class="stockcode">
    @Model.CCCStockCode
   </td>
   <td class="name">
    @Model.Name
   </td>
   <td class="pack">
    @Model.CCCPack
   </td>
   <td class="price">
    @Html.Partial("_ProductPrice", Model.ProductPrice)
   </td>
   <td class="MRP">
   £@Model.CCCMRP
   </td>
   <td>
   @*(1)Richard Evans Description = Input Button for Inserting indivdual Product's Into the Cart.*@
   <input type="text" id="quantity_@(Model.Id)" value="" size="1" />
   </td>
   </tr>


I can also post the Action and Route that has been wrote?

Thank you Kindly
11 years ago
Why don't you use POST (not GET) and process it on the server side similar to how it's implemented on the shopping cart page?
11 years ago
Thank you

Ill be honest I'm a novice so I don't know if this is the correct action. I guess you understand what we are trying to achieve

I updated the CategoryTemplate to the following


@using (Html.BeginRouteForm("ConcordAddMultipleProductsToCart", FormMethod.Post))
     {
     <tr class = "addtocart">
     <td>
     <input type="submit" name="Order" value="@T("Order")" id="Order" class="productlistaddtocartbutton" />
     </td>
     </tr>
     }


And the action which is based in both the Catalog and ShoppinCart Controller too that I took from various parts of your controllers.


//Richard Evans Description = Updating shoppingcart controller for B2T purposes as asked by Concord.
[HttpPost, ActionName("Product")]
[ValidateInput(false)]
        public ActionResult ConcordAddMultipleProductsToCart(int productId, FormCollection form)
        {
            //current we support only ShoppingCartType.ShoppingCart
            const ShoppingCartType shoppingCartType = ShoppingCartType.ShoppingCart;

            string returnUrl = _workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.LastContinueShoppingPage);

            var product = _productService.GetProductById(productId);
            if (product == null)
                //no product found
                return Redirect(returnUrl);
            
            int Id = 0;
            foreach (var formValue in form.AllKeys)
                if (formValue.StartsWith("Order-", StringComparison.InvariantCultureIgnoreCase))
                    Id = Convert.ToInt32(formValue.Substring("Order-".Length));


            var productVariants = _productService.GetProductVariantsByProductId(productId);
            if (productVariants.Count != 1){}

            //get default product variant
            var defaultProductVariant = productVariants[0];

            //quantity to add
            var qtyToAdd = defaultProductVariant.OrderMinimumQuantity > 0 ?
                defaultProductVariant.OrderMinimumQuantity : 1;

            //get standard warnings without attribute validations
            //first, try to find existing shopping cart item
            var cart = _workContext
                .CurrentCustomer
                .ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == shoppingCartType)
                .ToList();
            var shoppingCartItem = _shoppingCartService
                .FindShoppingCartItemInTheCart(cart, shoppingCartType, defaultProductVariant);
            //if we already have the same product variant in the cart, then use the total quantity to validate

            var quantityToValidate = shoppingCartItem != null ?
                shoppingCartItem.Quantity + qtyToAdd : qtyToAdd;

            var addToCartWarnings = _shoppingCartService
                .GetShoppingCartItemWarnings(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart,
                 defaultProductVariant, string.Empty, decimal.Zero, quantityToValidate, false);

            if (addToCartWarnings.Count > 0)
            {
                return Redirect(returnUrl);
            }

            //now let's try adding product to the cart (now including product attribute validation, etc)
            addToCartWarnings = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
                defaultProductVariant, ShoppingCartType.ShoppingCart,
                string.Empty, decimal.Zero, qtyToAdd, true);

            if (addToCartWarnings.Count > 0)
            {
                return Redirect(returnUrl);
            }

            return Redirect(returnUrl);
        }
        //Finsh


My Route is as follows

routes.MapLocalizedRoute("ConcordAddMultipleProductsToCart",
                                     "cart/addproduct/{productId}",
                            new { controller = "Catalog", action = "ConcordAddMultipleProductsToCart" },
                            new { productId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });


And nothing happens it
return Rediect(returnUrl)
as asked but no product's where inserted into the cart.
In the List I added 2 product to ProductId 1 and 3 to ProductId 2 as an small example?

Is this the correct procedure you where asking about

All help highly/humbly appreicated!
11 years ago
wertyuio1 wrote:
I guess you understand what we are trying to achieve

No. It's not clear. According to your route name (ConcordAddMultipleProductsToCart) you try adding multiple products with distinct quantities with one click. But according to its parameters only one product is added. ???

I also see that you asked this question in this topic (looks very similar). I agree with AndyMcKenna in this post: "it's becoming a huge thing". It can take a lot of time. Sorry. I'm quite busy with other tasks now and don't have an opportunity to help with it
11 years ago
Okay not a worry. Thank you ill get there..
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.