0

I have one form when I return all products. And next to everyone product I have TextBox.

 View form:    
|ID|Product|TextBox (amount of product)
|3 |Carrot | 20 
|4 |Potatos| 5 
|7 |Tomato | 10 

In this textbox I write a amount of the products and save this to database

 Add(zuzycie) table:    
|ID|ID_Product|amount|date
|1 | 3        | 20   |3.12.2011
|2 | 4        | 5    |3.12.2011
|3 | 7        | 10   |3.12.2011

and sum amount of product in other (product) databse.

Product table   
|ID|name   |amount
|3 |Carrot |10 + 20
|4 |Potatos|15 + 5
|7 |Tomato |7 + 10

In database, amount are save as seperate record. I hope you are understand what I have meaning.

How to get ID of everyone products in controller and save it in database?

View:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Zużycie produków</legend>
        <br />
        @{ 
    var grid = new WebGrid(ViewBag.produkty,null, "Produkty", 5);
              }
@grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
          grid.Column("nazwa_prod","Produkt"),
           grid.Column("ilosc","Ilość"),
           grid.Column("jednostka","Jed."),
           grid.Column("cena","Cena"),
           grid.Column("nazwa","Firma"),
           grid.Column("ID_Produkt", "ID_Prod" ),
           grid.Column(header: "Zuzycie", format: (item) =>
                        new HtmlString(
                                Html.TextBoxFor(model => model.ilosc).ToString())) /*amount text box */


        )
        )

Model:

public class zuzycieModel
    {
        public int ID_Produktu { get; set; } //Id_product
        public decimal ilosc {get;set;}  //amount
        public string data { get; set; } //date

    }

Controller:

public ActionResult zuzycie()
        {
            var prod = (from d in baza.Produkts
                        join s in baza.Firmas on d.ID_firma equals s.ID_firma
                        select new { d.ID_firma, d.nazwa_prod, d.ilosc, d.jednostka, d.cena, d.ID_Produkt, s.nazwa }).ToList();


            ViewBag.produkty = prod;
            return View();
        }

        [HttpPost]
        public ActionResult zuzycie(zuzycieModel model)
        {
            Zuzycie zuz = new Zuzycie(); //table zuzycie
            var dat = DateTime.Today;

            zuz.ID_Produkt = model.ID_Produktu; //Id product
            zuz.ilosc = model.ilosc; //amount
            zuz.data = dat; //date

            baza.Zuzycies.InsertOnSubmit(zuz);
            baza.SubmitChanges();

            return RedirectToAction("zarzadzaj_produktami", "Produkt");
        }

I try get ID of product, I putted it in View as HiddenFor and get using model... but it doesn't work...

user1031034
  • 836
  • 1
  • 14
  • 38

1 Answers1

0

I'm not clear on what was your idea with HttpPost zuzycie() method. What is Zuzycie() ? You're asking about saving multiple records but your ZuzycieModel cleary refers to a single ProductID.

Why are you using a ViewBag to pass the collection to your View ? can't you just return it through

return View(prod);  

Notice what are you passing - Collection (a List<>) and I don't see this to be reflected in your data persistance logic.

Paweł Staniec
  • 3,151
  • 3
  • 29
  • 41
  • I understand. So if I want to do this, what I need to do. I mean what term... controls... somethink what will guide me how start, what read about that. I using a ViewBag to show table in view. Why this.. I don't now... I wanted show records in table in View and it worked, so I still using this. I using `return View(prod); ` when I put date to e.g. in TextBox. – user1031034 Dec 03 '11 at 21:13
  • take a look at this question : http://stackoverflow.com/questions/4720330/model-containing-list-of-models-mvc-3-razor plus if you really feel lost complete this tutorial, it's a perfect way to understand main MVC concepts http://mvcmusicstore.codeplex.com/ – Paweł Staniec Dec 03 '11 at 21:22