在MVC中存储多个复选框值

时间:2015-11-18 18:58:19

标签: c# asp.net-mvc checkbox asp.net-mvc-5

以下是在数据库中添加新产品的完整代码,它可以正常运行。

I've 3 tables "Product","Category" and "SubCategory".

I've 5 Categories "Electronics","Clothing","Sports","Books" and "Others".

我想添加3个大小的复选框:小,中,和我想要隐藏这些复选框,除非我从下拉列表中选择“服装”类别。当我从类别下拉列表中选择服装时,应该出现尺寸复选框,这样我就可以选择尺寸:小复选框或者可以是尺寸:小尺寸:中等复选框,我希望将其存储在数据库中。我认为复选框的两个值都不能存储在一行数据库中。

示例

productid = 1

product name= polo t-shirt

sizes: small & medium (both checkboxes are checked)

并且在查看添加到购物车功能的产品详细信息时,我想要下拉列表中的可用尺寸。

我有产品型号

public int ProductId { get; set; }

    public int? CategoryId { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    public string Description { get; set; }

    public decimal? Price { get; set; }

    public int? Quantity { get; set; }

    [StringLength(100)]
    public string ImagePath { get; set; }

    public DateTime? Submitted { get; set; }

    public int? StoreId { get; set; }

    [StringLength(50)]
    public string DeliveryDate { get; set; }

    [StringLength(50)]
    public string ShippingCharges { get; set; }

    public int? SubCategoryId { get; set; }

    public int? ProvinceId { get; set; }

    public int? CityId { get; set; }

    [StringLength(50)]
    public string PaymentMethod { get; set; }

    public virtual Category Category { get; set; }

    public virtual City City { get; set; }

    public virtual Province Province { get; set; }

    public virtual Store Store { get; set; }

    public virtual SubCategory SubCategory { get; set; }

产品控制器

[HttpPost]
    [Authorize(Roles = "StoreOwner")]
    [ValidateAntiForgeryToken]
    public ActionResult AddProduct(HttpPostedFileBase file)
    {
        List<string> DeliveryDate = new List<string>();
        DeliveryDate.Add("1-2 Days");
        DeliveryDate.Add("3-5 Days");
        DeliveryDate.Add("1 Week");
        SelectList dd = new SelectList(DeliveryDate);
        ViewBag.DeliveryDate = dd;

        List<string> PaymentMethods = new List<string>();
        PaymentMethods.Add("Cash on Delivery");
        SelectList pm = new SelectList(PaymentMethods);
        ViewBag.PaymentMethods = pm;

        IEnumerable<SelectListItem> provinces = db.Provinces.Select(c => new SelectListItem
        {
            Value = c.ProvinceId.ToString(),
            Text = c.ProvinceName

        });
        ViewBag.ProvinceId = provinces;

        IEnumerable<SelectListItem> cities = db.Cities.Select(c => new SelectListItem
        {
            Value = c.CityId.ToString(),
            Text = c.CityName

        });
        ViewBag.CityId = cities;

        IEnumerable<SelectListItem> categories = db.Categories.Select(c => new SelectListItem
        {
            Value = c.CategoryId.ToString(),
            Text = c.Name

        });

        ViewBag.CategoryId = categories;

        IEnumerable<SelectListItem> subcategories = db.SubCategories.Select(c => new SelectListItem
        {
            Value = c.SubCatId.ToString(),
            Text = c.SubCatName

        });
        ViewBag.SubCategoryId = subcategories;


        IEnumerable<SelectListItem> stores = db.Stores.Select(c => new SelectListItem
        {
            Value = c.StoreId.ToString(),
            Text = c.Name

        });
        ViewBag.Stores = stores;

         if (file != null)
            {
                string ImagePath = System.IO.Path.GetFileName(file.FileName);
                string physicalPath = Server.MapPath("~/ProductImages/" + ImagePath);

                file.SaveAs(physicalPath);

                //save new record in database
                Product newRecord = new Product();
                newRecord.Name = Request.Form["Name"];
                newRecord.CategoryId = Convert.ToInt32(Request.Form["CategoryId"]);
                newRecord.SubCategoryId = Convert.ToInt32(Request.Form["SubCategoryId"]);
                newRecord.StoreId = Convert.ToInt32(Request.Form["Stores"]);
                newRecord.Description = Request.Form["Description"];
                newRecord.Price = Convert.ToDecimal(Request.Form["Price"]);
                newRecord.Quantity = Convert.ToInt32(Request.Form["Quantity"]);
                newRecord.ProvinceId = Convert.ToInt32(Request.Form["ProvinceId"]);
                newRecord.CityId = Convert.ToInt32(Request.Form["CityId"]);
                newRecord.ShippingCharges = Request.Form["ShippingCharges"];
                newRecord.DeliveryDate = Request.Form["DeliveryDate"];
                newRecord.PaymentMethod = Request.Form["PaymentMethods"];
                newRecord.ImagePath = ImagePath;
                newRecord.Submitted = DateTime.Now;
                db.Products.Add(newRecord);
                db.SaveChanges();
                return RedirectToAction("OwnerManage","Manage");
            }

        return View();
    }

查看

@using (Html.BeginForm("AddProduct", "Store", FormMethod.Post, new { enctype = "multipart/form-data",   @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new product.</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label", data_val_required = "required" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        @Html.ValidationMessageFor(m=>m.Name)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Description, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextAreaFor(m => m.Description, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.CategoryId, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownListFor(x => x.CategoryId, ViewBag.CategoryId as SelectList, new { @class = "CssCategory" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.SubCategoryId, new { @class = "col-md-2 control-label subcatshow" })
    <div class="col-md-10">
        @Html.DropDownListFor(x => x.SubCategoryId, ViewBag.SubCategoryId as SelectList, new { @class = "CssSubCategory" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.StoreId, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownList("Stores", "Select a Value")
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.ProvinceId, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownListFor(x => x.ProvinceId, ViewBag.ProvinceId as SelectList, new { @class = "CssProvince" })

    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.CityId, new { @class = "col-md-2 control-label cityshow" })
    <div class="col-md-10">
        @Html.DropDownListFor(x => x.CityId, ViewBag.CityId as SelectList, new { @class = "CssCity" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.Price, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(x => x.Price, new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.Quantity, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(x => x.Quantity, new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.DeliveryDate, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownList("DeliveryDate", ViewBag.DeliveryDate as SelectList)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.ShippingCharges, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(x => x.ShippingCharges, new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.PaymentMethod, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownList("PaymentMethods", ViewBag.PaymentMethods as SelectList)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.ImagePath, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        <input type="file" name="file" id="file" style="width: 100%;" />
    </div>
</div>

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

}

1 个答案:

答案 0 :(得分:3)

你可以做很多事情。一对夫妇是:

一个。您可以向产品对象添加名为small medium和large的类型bool?的三个属性。

B中。您可以使用类型为bool?的三个大小属性创建大小对象,然后将大小对象添加到产品对象中。

然后在视图上,您​​可以使用jquery根据所选的产品类型切换其可见性。

这取决于你想要如何处理它。

一个。如果您只需要在表中使用一行,则可以存储要解析的字符串,如“S; L”,并按';'分割/连接。

B中。你也可以做一些荒谬的事情并创建一个包含所有可能的选择变体的新表,并为每个表分配一个ID并将ID传递到另一个表的单行中。

℃。我个人会创建一个新的表,将产品ID作为外键,每个大小有3列作为bool

但是,我不确定将哪种方式视为“良好做法”。