1

I'm having the same problem as this question

I have a product table with columns

(PK)Product_Id, (FK)Category_Id, Name, Description, Size, Color, Quantity, Price, Condition

For now I'm storing these values through a Simple form with all fields.

Model Product.cs

public partial class Product
{
    [Key]
    public int ProductId { get; set; }

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

    [AllowHtml]
    public string Description { get; set; }

    public decimal? Price { get; set; }

    public int? Quantity { get; set; }

    public string Condition { get; set; }

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

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

View Sizes and Colors are stored by simple checkbox.

@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.Size, new { @class = "col-md-2 control-label sizecheckboxshow" })
    <div id="sizecheckboxes" class="col-md-10">
        <input type="checkbox" name="Size" value="XS" /><span class="sizecheckboxtext">XS</span>
        <input type="checkbox" name="Size" value="S" /><span class="sizecheckboxtext">S</span>
        <input type="checkbox" name="Size" value="M" /><span class="sizecheckboxtext">M</span>
        <input type="checkbox" name="Size" value="L" /><span class="sizecheckboxtext">L</span>
        <input type="checkbox" name="Size" value="XL" /><span class="sizecheckboxtext">XL</span>
        <input type="checkbox" name="Size" value="XXL" /><span class="sizecheckboxtext">XXL</span>
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.Colors, new { @class = "col-md-2 control-label colorcheckboxshow" })
    <div id="colorcheckboxes" class="col-md-10">
        <input type="checkbox" name="Color" value="Red" /><span class="colorcheckboxtext">Red</span>
        <input type="checkbox" name="Color" value="Green" /><span class="colorcheckboxtext">Green</span>
        <input type="checkbox" name="Color" value="Blue" /><span class="colorcheckboxtext">Blue</span>
        <input type="checkbox" name="Color" value="Black" /><span class="colorcheckboxtext">Black</span>
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Condition, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.RadioButtonFor(x => x.Condition, "New") <text>New</text>
        @Html.RadioButtonFor(x => x.Condition, "Used") <text>Used</text>
    </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">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Create Product" />
    </div>
</div>
}

Controller

storing the products using this code

[HttpPost]
public ActionResult AddProduct(Product newRecord)
    {
                    newRecord.Name = Request.Form["Name"];
                    newRecord.CategoryId = Convert.ToInt32(Request.Form["CategoryId"]);
                    newRecord.Size = Request.Form["Size"];
                    newRecord.Colors = Request.Form["Color"];
                    newRecord.Description = Request.Unvalidated.Form["Description"];
                    newRecord.Price = Convert.ToDecimal(Request.Form["Price"]);
                    newRecord.Quantity = Convert.ToInt32(Request.Form["Quantity"]);
                    db.Products.Add(newRecord);
                    db.SaveChanges();
                    return RedirectToAction("Index", "Home");
     }

and the value is stored in 1 row. How can I store Sizes with quantities ? As the referenced question.

Kindly check this link also for more understanding of this question, When you move the mouse on Sizes it shows the left items in stock. I want to achieve this .

Community
  • 1
  • 1
user3223395667
  • 239
  • 1
  • 6
  • 16
  • Why are you using `Request.Form[..]` to get the values when you already have a model? And what is you model? And what are you attempting to store in the `Size` and `Color` fields (a comma separated list of values)? –  Jan 14 '16 at 09:38
  • Why? You should have one table for the product (ID, description, price etc) and one table for the sizes and associated stock quantity (not sure where color fits into it) –  Jan 14 '16 at 09:47
  • I'm in phase of learning, but I'm getting the result with comma separated, I know there must be many other ways to store these values. Just drop the color attribute, how can i store sizes with many quantities ? At the end of the question kindly check that link, and move your mouse to the SIZES, it shows left items. I want to do this. – user3223395667 Jan 14 '16 at 09:50
  • Then you need to rethink you database design. No time at the moment, but I will add an answer later if no one else does. –  Jan 14 '16 at 09:53
  • @StephenMuecke should I make another table of Sizes (lets just drop color for now) ? SizesId, SizeName. but I'm still confuses on storing the quantity. Suppose I have a Shirt with S,M,L sizes and Quantity of S=10, M=5 and L=20, where I'm going to store these values and how ? – user3223395667 Jan 14 '16 at 10:00
  • Yes, you should have another table - but give me an hour :) –  Jan 14 '16 at 10:05

1 Answers1

4

Your database and model design is not correct for what your trying to achieve. You need one table for the product (say Product), containing common properties such as ID, Name, Description and Price (assuming the price do not vary with the size), and another table (say `ProductStock) for the Size and StockQuantity (with a FK to the Products table).

Your models would be something like

public class Product
{
  public int ID { get; set; }
  public string Name { get; set; }
  public string Description { get; set; }
  public decimal Price { get; set; }
  public List<ProductStock> Stock { get; set; }
}
public class ProductStock
{
  public int ID { get; set; }
  public string Size { get; set; } // and enum may be better if the possible values wont change
  public int Quantity { get; set; }
}

Then your view would be something like

@model Product
<h2>@Html.DisplayFor(m => m.Name</h2>
@Html.DisplayFor(m => m.Description)
@Html.DisplayFor(m => m.Description)
@foreach(var item in Stock)
{
  @Html.DisplayFor(m => item.Size)
  @Html.DisplayFor(m => item.Quantity)
}
  • Thanks for your answer but how can I store using the controller ? and then display in the productDetails View page. – user3223395667 Jan 14 '16 at 11:20
  • That's too much to add to this answer. For creating/editing a product, you could use the techniques in the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) to dynamically add the rows for size/quantity and save the product and its collection of stock in one form/controller. –  Jan 14 '16 at 11:25
  • For displaying the product, I have shown a simplified view (obviously the site you linked to is generating `` elements with borders etc and a `title` attribute to display the available quantity in a tooltip). I have no idea if your using EF or some other repository code but the controller for it might be something like `public ActionResult Details(int ID) { Product model = db.Products.Include("Stock").Where(p => p.ID == ID).FirstOrDefault(); return View(model); }` –  Jan 14 '16 at 11:30
  • on the Save product button I need to store products in both tables ? Products & ProductsStock and then display in the productdetails pages ? – user3223395667 Jan 14 '16 at 11:31
  • Yes - refer [this answer](http://stackoverflow.com/questions/7877649/model-updates-to-contained-collection-not-saved-in-db-when-saving-parent-entity) for an example in EF. Note also you current design in too rigid - what happens if a new size (or color) is added (or removed) from the collection - you have hardcoded then into the view. Which is another reason you need the second table and the ability to add remove stock items as well as change the quantity. –  Jan 14 '16 at 11:42
  • Thanks for the help your answer really guided and helped me a little but still I'm little confused between how to store data in both, products & productstock, tables through controller. I've been searching for this query for 1 week and my mind is full confused, no code example found, all shopify and other cats plugins. I only want to display product like this https://www.daraz.pk/bata-black-leather-slip-on-dress-shoes-for-men-8546511-5410120.html Sizes with item left. – user3223395667 Jan 14 '16 at 16:24
  • There is not nearly enough information to answer that. The link I gave your in the last comment is one example. If your having problems, I suggest you ask a new question for more detail and what you have tried. If however you having problems with creating a UI for the sizes similar to the site you linked to, let me know and I can create a example. –  Jan 16 '16 at 04:05