针对特定商店的类别模型的多项选择ASP.Net MVC

问题描述

我想针对选定的商店标记多个类别。目前,使用我的功能,每个商店只能分配1个类别。但是,我希望以一种可以为任何商店分配多个类别的方式编写代码。 示例:Amazon Store应该具有多个类别,例如时尚,智能手机,电子产品等等。我目前的代码只分配给了智能手机。

  1. 商店模型类
public class Store
    {

        [Key]
        public int StoreID { get; set; }
        [required]
        public string StoreName { get; set; }
        public string Storelogo { get; set; }
        [AllowHtml]
        public string TopDesc { get; set; }
        [AllowHtml]
        public string MainDesc { get; set; }
        public int CatID { get; set; }
        public string CatName { get; set; }
        public string Slug { get; set; }        
        public string MetaDesc { get; set; }

        [ForeignKey("CatID")]
        public virtual Category Category { get; set; }

        public IEnumerable<SelectListItem> Categories { get; set; }
    }
  1. 类别模型类
public class Category
    {
        [Key]
        public int CatID { get; set; }
        public string CatName { get; set; }
        public string Slug { get; set; }
        public string CatIcon { get; set; }
    }
  1. AddStore视图
@model C4A.Models.VM.Store

@{
    ViewBag.Title = "AddStore";
}

<h2>AddStore</h2>


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

    <div class="form-horizontal">

        @Html.ValidationSummary(true,"",new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.StoreName,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StoreName,new { htmlAttributes = new { @class = "form-control col-md-12 col-sm-2" } })
                @Html.ValidationMessageFor(model => model.StoreName,new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.MetaDesc,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.MetaDesc,new { htmlAttributes = new { @class = "form-control col-md-12 col-sm-2" } })
                @Html.ValidationMessageFor(model => model.MetaDesc,new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2">Upload Image</label>
            <div class="col-md-10">
                <input type="file" name="file" id="file" class="btn btn-info" />
                @Html.ValidationMessageFor(model => model.Storelogo,new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TopDesc,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.TopDesc,new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.TopDesc,new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.MainDesc,htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.MainDesc,new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.MainDesc,new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2">Category</label>
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.CatID,Model.Categories,"Select Categories",new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CatID,new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List","Index")
</div>

@section Scripts {
    <script src="~/Scripts/jquery-3.5.1.min.js"></script>
    @Scripts.Render("~/bundles/jqueryval")

    <script src="~/Scripts/SideBar.js"></script>
    <script src="~/Scripts/ckeditor/ckeditor.js"></script>

    <script>
        CKEDITOR.replace("TopDesc");
        CKEDITOR.replace("MainDesc");
    </script>
}
  1. 控制器
[HttpGet]
        public ActionResult AddStore()
        {
            Store model = new Store();
            using (Db db = new Db())
            {
                model.Categories = new SelectList(db.Categories.ToList(),"CatID","CatName");
            }
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddStore(Store model,HttpPostedFileBase file)
        {
            if (!ModelState.IsValid)
            {
                using (Db db = new Db())
                {
                    model.Categories = new SelectList(db.Categories.ToList(),"CatName");
                    return View(model);
                }
            }

            using (Db db = new Db())
            {
                if (db.Stores.Any(x => x.StoreName == model.StoreName))
                {
                    ModelState.AddModelError("","Store name already taken");
                    model.Categories = new SelectList(db.Categories.ToList(),"CatName");
                    return View(model);
                }
            }

            int id;

            using (Db db = new Db())
            {
                Store dto = new Store();

                dto.StoreName = model.StoreName;
                //dto.Storelogo = model.Storelogo;
                dto.TopDesc = model.TopDesc;
                dto.MainDesc = model.MainDesc;
                dto.CatID = model.CatID;
                dto.Slug = model.StoreName.Replace(" ","-").ToLower();
                dto.MetaDesc = model.MetaDesc;

                Category catDto = db.Categories.FirstOrDefault(x => x.CatID == model.CatID);
                dto.CatName = catDto.CatName;

                db.Stores.Add(dto);
                db.SaveChanges();

                id = dto.StoreID;
            }
            TempData["SM"] = "Store Added Successfully";

            #region Upload Image
            string StoreImageName = Path.GetFileName(file.FileName) + ".webp";
            string physicalPath = Server.MapPath("~/Images/" + StoreImageName);

            if (file != null)
            {
                // Get file extension
                string ext = file.ContentType.ToLower();

                // Verify extension
                if (ext != "image/jpg" &&
                    ext != "image/jpeg" &&
                    ext != "image/pjpeg" &&
                    ext != "image/gif" &&
                    ext != "image/x-png" &&
                    ext != "image/webp" &&
                    ext != "image/png")
                {
                    using (Db db = new Db())
                    {
                        model.Categories = new SelectList(db.Categories.ToList(),"CatName");
                        ModelState.AddModelError("","The image was not uploaded - wrong image extension.");
                        return View(model);
                    }


                }

                // Save image name to DTO
                using (Db db = new Db())
                {
                    Store dto = db.Stores.Find(id);
                    dto.Storelogo = StoreImageName;

                    db.SaveChanges();
                }
                file.SaveAs(physicalPath);
            }
            #endregion
            return RedirectToAction("AllStore");
        }

有人可以提供代码帮助,以便我可以为特定商店分配多个类别

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)