lundi 16 février 2015

Getting multiple check box values and put them into ICollection MVC

I want to know how I can retrieve the values of multiple checkboxes. I have a diagram and some code, please take a look at it. There's a few steps that I need to do:



  1. Put all categories from the Category table in the Product/Create file as Checkboxes (which will put the input in the Product table)

  2. Retrieve the chosen Checkboxes

  3. for each chosen checkbox, add the product to it's own list in the category table


Later on I have to display every category with their corresponding products.


I hope that makes sense. Here's my code. I do not want to create another file/model/view/controller, if possible. I translated everything to English so it's easier to read.


Category Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebshopMVC.Models
{
public class Category
{
public int ID { get; set; }
public string Image { get; set; }
public string Name { get; set; }
public string Description{ get; set; }

public virtual ICollection<Product> Producten { get; set; }
}
}


Product Model:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebshopMVC.Models
{
public class Product
{
public int ID { get; set; }
public string Image { get; set; }
public string Name{ get; set; }
public string Description{ get; set; }
public decimal Price{ get; set; }

public virtual ICollection<Category> Categories{ get; set; }

public virtual ICollection<Offer> Offers{ get; set; }

}
}


Product/Create View:



@model WebshopMVC.Models.Categorie

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Categorie</h4>
<hr />
@Html.ValidationSummary(true)

<div class="form-group">
@Html.LabelFor(model => model.Image, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Image)
@Html.ValidationMessageFor(model => model.Image)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</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>


Product Controller(Create) public ActionResult Create() { return View(); }



// POST: /Product/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://ift.tt/1eddaz0.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Image,Name,Description,Price,Categories")] Product product)
{
if (ModelState.IsValid)
{
db.Producten.Add(product);

db.SaveChanges();
return RedirectToAction("Index");
}

return View(product);
}


And here's a Diagram of how it should be:


Diagram


It's just an Image and it's in Dutch, so I will give you the English translation:



Categorie => Category
plaatje => Image
naam => Name
omschrijving => Description

Product => Product
plaatje => Image
naam => Name
omschrijving => Description
Prijs => Price




So, again:




  1. Let user create a product and assign 1 or more categories, which will come out of the Categories Table




  2. Get the selected categories, add them to the Categories list in the Product Model




  3. Add the product to the Products list in the Categories model




  4. Later on I have to be able to loop through the categories and get their products, so I can link to them strong text




Steps 2 and 3 may seem a bit weird, but according to the model, a category has * products and a product has 1 or more categories, so I also coded it that way


I have been struggling with this for many hours now. If anyone could help me out I would be so thankful!


If you need any other information, I will gladly give it to you!





Aucun commentaire:

Enregistrer un commentaire