Developing new Product Attribute?

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

I'm new to programming and just jumped from 1.9 to 2.40. After already trying my best to learn 1.9, I was abruptly slapped with 2.40 and the MVC 3 type of platform it is now using. Not even sure if that is the appropriate term to use. I understand the MVC model, but still have issues with models. Should I just be getting and setting values?

Nonetheless, I am attempting to create a new tab that is found on product edits. I think these are ProductAttributes. I am attempting to create a controller in which an admin can upload files and list the files if they already exist. I've been looking on youtube and so on for tuts on file upload classes. I was just attempting a real simple one at this time to see if it feasible for me. Below is my code:

CSHTML File:

@{
    ViewBag.Title = "ProductDocuments";
}

<h2>Product Documents</h2>

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="Submit" />
}


Controller File:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Nop.Admin.Models.Catalog;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Services.Catalog;
using Nop.Services.Common;
using Nop.Services.ExportImport;
using Nop.Services.Localization;
using Nop.Services.Logging;
using Nop.Services.Media;
using Nop.Services.Security;
using Nop.Services.Tax;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Mvc;
using Telerik.Web.Mvc;

namespace Nop.Admin.Controllers
{
    public class ProductDocumentsController : BaseNopController
    {

        private readonly IImportManager _importManager;
        private readonly ICustomerActivityService _customerActivityService;
        private readonly IPermissionService _permissionService;
        private readonly ILocalizationService _localizationService;
        //
        // GET: /ProductDocuments/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ImportFile(FormCollection form)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
                return AccessDeniedView();

            try
            {
                var file = Request.Files["file"];
                if (file != null && file.ContentLength > 0)
                {
                    var fileBytes = new byte[file.ContentLength];
                    file.InputStream.Read(fileBytes, 0, file.ContentLength);
                    //do stuff with the bytes
                    string fileName = string.Format("{0}.doc", file);
                    string filePath = string.Format("{0}content\\files\\docs\\{1}", Request.PhysicalApplicationPath, fileName);

                    System.IO.File.WriteAllBytes(filePath, fileBytes);
                    _importManager.ImportProductsFromXlsx(filePath);
                }
                else
                {
                    ErrorNotification("Please upload a file");
                    return RedirectToAction("List");
                }
                SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Imported"));
                return RedirectToAction("List");
            }
            catch (Exception exc)
            {
                ErrorNotification(exc);
                return RedirectToAction("List");
            }

        }

        public ActionResult List()
        {
            return View("Index");
        }

    }
}



Now, most of the controller class I used from the ProductController already from the ImportExcel Method. I thought this would work. However, since this is part of the admin and not supposed to be live, how do I at least view it first, just to see if it works?

From there, I would want to add it to the ProductEdit page where there are several tabs. How would I go about doing that too? I do apologize for the MANY questions. If there is a tut out there or any advice on this, I would greatly appreciate it. nopCommerce is awesome, I would just love to learn how to program for it.

Thanks a bunch!
12 years ago
Hello nopCommerce community,

I thought over the weekend I would have been able to receive at least some insightful information but it looks like no one has the answer just yet? Allow me to edit my post nonetheless since I have been able to poke around more today and found exactly where to add a new tab as well as display exactly what I need. I do still need help and would very much appreciate any tips in getting this extra tab to work appropriately.

Also, let me rephrase my topic title, it isn't a product attribute I'm attempting to develop, but just the ability to upload a file that is connected to the specific item I am editing.

Here are my revisions:

1.  How to add a tab to Product Edit:
        a.  Inside Nop.Admin
                i.  Navigate to Views > _CreateOrUpdate.cshtml
        b.  Add TabPanel after line 24
                i.
x.Add().Text(T("Admin.Catalog.Products.ProductDocuments").Text).Content(TabProductDocuments().ToHtmlString());
  
        c.  Create ‘TabProductDocuments’ help method on line 772
                i.  
@helper TabProductDocuments()
{
if (Model.Id > 0)
{
<h2>Product Documents</h2>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
}
else
{
@T("Admin.Catalog.Products.ProductDocuments.SaveBeforeEdit")
}
}

      
        d.  Change ProductDocumentsController.cs to more simple code:
                i.  
public class ProductDocumentsController : BaseNopController
{
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
  var fileName = Path.GetFileName(file.FileName);
  var path = Path.Combine(HttpContext.Server.MapPath("../Content/files/uploads"), fileName);
  file.SaveAs(path);
}
  return RedirectToAction("Index");
}


Now, the issue I am experiencing is: I can see the tab now in Product Edit, but I cannot upload the file. It submits the query but just refreshes the page and leads back to Product List. No file is uploaded. If you can, please assist me with trying to upload the file properly to the path I have designated. Thank you again for your assistance.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.