mercredi 18 novembre 2015

Store Multiple checkboxes values in MVC

Below is the complete code of adding a new product in the database and it works.

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

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

I want to add a 3 checkboxes of sizes: small,medium & large and I want these checkboxes as hidden unless I select "Clothing" category from dropdownlist. When I select Clothing from category dropdown the size checkboxes should appear so I can select size:small checkbox or may be both size:small and size:medium checkboxes and I want this to be stored in database. I don't think both values of checkboxes can be stored in one single row of database.

Example productid = 1

product name= polo t-shirt

sizes: small & medium (both checkboxes are checked)

and I want the available sizes in a dropdownlist when viewing the product details for add to cart function.

I've a product model:

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; }

product controller

[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();
    }

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>

}




Aucun commentaire:

Enregistrer un commentaire