mardi 5 janvier 2016

create session to use values inside the same method

This is code snippet for load 15 text areas into an Add_New_Product form page.

this is the view enter image description here

Since in this form page has many text areas(like 15) ,I limited with pagination , one text area per one page.

    public ActionResult Add_Product(int? page)
    {
        var dummyItems = db.AB_ProductTypeCategoryField;
        var pager = new PaginationModel.Pager(dummyItems.Count(), page);

        var model = new AddNewProduct
        {
            ListProductFields = dummyItems.OrderBy(i => i.ProductFieldID).Skip((pager.CurrentPage - 1) * pager.PageSize).Take(pager.PageSize).ToList(),
            Pager = pager
        };

        return View(model);
}

I want use above ListProductFields data iteratvely inside same above method. Since , once I insert the data to first text area and then go to next text area , again then I click previous button , then those initially added values for first text area are disappearing .I decided to come up with sessions , So I changed above code as follows

    public ActionResult Add_Product(int? page, AddNewProduct sample)
    {
        AddNewProduct newmodel = sample;                       

        AddNewProduct newmodels = Session["TemplateData"] as AddNewProduct;

        var dummyItems = db.AB_ProductTypeCategoryField;
        var pager = new PaginationModel.Pager(dummyItems.Count(), page);

        var model = new AddNewProduct
        {
            ListProductFields = dummyItems.OrderBy(i => i.ProductFieldID).Skip((pager.CurrentPage - 1) * pager.PageSize).Take(pager.PageSize).ToList(),
            Pager = pager
        };

        Session["TemplateData"] = model;

        return View(model);

    }

But it doesn't keep any value

Aucun commentaire:

Enregistrer un commentaire