Categories/Subcategories in asp.net mvc

前端 未结 3 1013
慢半拍i
慢半拍i 2020-12-30 15:12

We are making a marketplace like https://www.etsy.com/. And we have a problem in categorising the listings. We want to categories the item in the Listing in 3 levels, f.ex i

3条回答
  •  情歌与酒
    2020-12-30 15:39

    I giving here a example for category and subcategory with image upload.

     public class ProductController : Controller
    {
        ApplicationDbContext db = new ApplicationDbContext();
        // GET: Product
        public ActionResult Index()
        {
    
            return View();
        }
    
        public ActionResult insert(int? id)
        {
            ViewBag.categoryList = db.Product.Where(x => x.CategoryId == 0).Select(x => new SelectListItem { Text = x.name, Value = x.Id.ToString() }).ToList();
            var product = db.Product.Where(x => x.Id == id).Select(x => x).FirstOrDefault();
            if (product == null) { product = new Product(); product.CategoryId = 0; }
            return View(product);
        }
        [HttpPost]
        public ActionResult insert(Product model)
        {
    
            if (Request.Files.Count > 0)
                if (Request.Files["fileupload"].ContentLength > 0)
                {
                    var fileupload = Request.Files[0];
                    var fileName = Path.GetFileName(fileupload.FileName);
                    model.Imagename = fileName;
                    model.ImageUrl = DateTime.Now.Ticks.ToString() + "." + fileName.Split('.')[1];
    
                    string baseurl = Server.MapPath("/") + "Images/" + model.ImageUrl;
                    fileupload.SaveAs(baseurl);
                }
            if (model.Id > 0)
            {
                var productEntity = db.Product.Where(x => x.Id == model.Id).Select(x => x).FirstOrDefault();
                if (model.Imagename != null)
                    productEntity.Imagename = model.Imagename;
                if (model.ImageUrl != null)
                    productEntity.ImageUrl = model.ImageUrl;
                productEntity.name = model.name;
                productEntity.CategoryId = model.CategoryId;
            }
            else
            {
                db.Product.Add(model);
            }
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    
        public ActionResult ProductList()
        {
            var product = db.Product.Where(x => x.Id > 0).Select(x => x).ToList();
            return View(product);
        }
    
        public ActionResult getsubcategory(int id)
        {
            var list = db.Product.Where(x => x.CategoryId == id)
                         .Select(x => new SelectListItem { Text = x.name, Value = x.Id.ToString() }).ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    
    }
    

    This upper controller for insert update record.

    Below html code :

                    @model WebApplication1.Models.Product
    
                @{
                    ViewBag.Title = "insert";
                    Layout = "~/Views/Shared/_Layout.cshtml";
                }
    
                

    insert

    @using (Html.BeginForm("insert","product", FormMethod.Post,new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken()

    Product


    @Html.HiddenFor(x=>x.Id) @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.DropDownList("SubCategory", new SelectList(ViewBag.categoryList, "Value", "Text", Model.CategoryId), "-Select-", new { @onchange = "categoryselect()", htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
    @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.DropDownListFor(model => model.CategoryId, new SelectList(ViewBag.categoryList, "Value", "Text", Model.CategoryId),"-Select-", new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
    @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.TextBoxFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
    @Html.LabelFor(model => model.Imagename, htmlAttributes: new { @class = "control-label col-md-2" })
    @*@Html.(model => model.Imagename, new { htmlAttributes = new { @class = "form-control" } })*@ @Html.ValidationMessageFor(model => model.Imagename, "", new { @class = "text-danger" })
    }
    @Html.ActionLink("Back to List", "Index")

    model:

                    namespace WebApplication1.Models
                {
                    public class Product
                    {
                        [Key]
                        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
                        public int Id { get; set; }
                        public int CategoryId { get; set; }
                        public string name { get; set; }
                        public string ImageUrl { get; set; }
                        public string Imagename { get; set; }
                    }
    
                    public class Category
                    {
                        [Key]
                        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
                        public int Id { get; set; }
                        public int PrentId { get; set; }
                        public string name { get; set; }
    
                    }
                }
    

    Index Page:

                    @{
                    ViewBag.Title = "Index";
                    Layout = "~/Views/Shared/_Layout.cshtml";
                }
    
                

    Index

    List Page:

                    @model IEnumerable
    
                

    @Html.ActionLink("Create New", "Insert")

    @foreach (var item in Model) { }
    @Html.DisplayNameFor(model => model.CategoryId) @Html.DisplayNameFor(model => model.name) @Html.DisplayNameFor(model => model.ImageUrl) @Html.DisplayNameFor(model => model.Imagename)
    @Html.DisplayFor(modelItem => item.CategoryId) @Html.DisplayFor(modelItem => item.name) @Html.DisplayFor(modelItem => item.ImageUrl) @Html.DisplayFor(modelItem => item.Imagename) @Html.ActionLink("Edit", "insert", new { id=item.Id })

提交回复
热议问题