0

I'm trying to set a property Klimatogram.Locatie in a controller class like this:

public ActionResult SelectKlimatorgramVanLocatie(LocatieKlimatogramViewModel model)
{

Locatie selectedLocatie = (Locatie)Session["GevondenLocatie"];
       Klimatogram klimatogram = new Klimatogram(selectedLocatie);


       TempData["kilmatogram"] = klimatogram;

       return RedirectToAction("Index", "Vragen");
}

So if I debug I can see that klimatogram.Locatie is being set by selectedLocatie.

EDIT :

Now klimatogram.Locatie gets filled up. But when i look at the values from Klimatogram they are all 0.

Here is an example in the class Klimatogram:

public class Klimatogram
{
    public Klimatogram(Locatie selectedLocatie)
    {
       Locatie =selectedLocatie;
    }


    public Locatie Locatie { get; set; }

    public int Id { get; set; }

    private double warmsteMaand;
    private double aantalDrogeMaanden;
    private double gemJaarTemp;
    private double gemJaarNeerslag;
    private double hoeveelNeerslagWinter;
    private double hoeveelNeerslagZomer;
    private double koudsteMaand;

    public int klimaId { get; set; }




    public double TempWarmsteMaand
    {
        get { return warmsteMaand; }
        set
        {
            value = Locatie.TemperatuurPerMaand[0];
            for (int i = 1; i < 12; i++)
            {
                if (Locatie.TemperatuurPerMaand[i] > value)
                {
                    value = Locatie.TemperatuurPerMaand[i];
                }
            }
            warmsteMaand = value;
        }
    }

So this method doesn't get any value from my Locatie. When i debug it says it is 0 which is wrong.

this is my vragenController:

public ActionResult Index() {

        var klimatogram = (Klimatogram)TempData["kilmatogram"];

        VragenViewModel vragenViewModel = new VragenViewModel(klimatogram);

        return View("VragenControl",vragenViewModel);
    }

This is the viewmodel:

public class VragenViewModel {

//    public SelectList Maanden { get; set; }

    public VragenViewModel()
    {

    }

    public VragenViewModel(Klimatogram klima)
    {
        warm = klima.TempWarmsteMaand;
        koud = klima.TempKoudsteMaand;
        droge = klima.AantalDrogeMaanden;
        winterneerslag = klima.HoeveelheidNeerslagWinter;
        zomerneerslag = klima.HoeveelheidNeerslagZomer;
    }

    public double zomerneerslag
    { get; set; }

    public double winterneerslag { get; set; }

    public double droge { get; set; }

    public double koud { get; set; }

    public double warm { get; set; }


}
  • Are you saying `Locatie` is null in the `Index` View? Can you post the relevant code for the View? – markpsmith Mar 16 '15 at 10:26
  • If you don't need to use session, don't use it. Can you not pass this property using your viewmodel? What happens at different points in execution when you inspect the Session cache? – Inspector Squirrel Mar 16 '15 at 10:28

2 Answers2

0

maybe just

public ActionResult SelectKlimatorgramVanLocatie(LocatieKlimatogramViewModel model)
    {

        Locatie selectedLocatie = (Locatie)Session["GevondenLocatie"];
        Klimatogram klimatogram = new Klimatogram();
        klimatogram.Locatie = selectedLocatie;




        return Index(klimatogram);
    }
AiSatan
  • 158
  • 2
  • 10
0

The third argument of RedirectToAction is routeValues, not passing a model like you do with View.

You can use the TempData variable to pass data through as per this answer:

public ActionResult SelectKlimatorgramVanLocatie(LocatieKlimatogramViewModel model)
{

    Locatie selectedLocatie = (Locatie)Session["GevondenLocatie"];
    Klimatogram klimatogram = new Klimatogram();
    klimatogram.Locatie = selectedLocatie;

    TempData["kilmatogram"] = kilmatogram;

    return RedirectToAction("Index", "Vragen");
}

// ...

public ActionResult Index()
{
    var kilmatogram = (Kilmatogram)TempData["kilmatogram"];

    // ...
}
Community
  • 1
  • 1
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • Thanks. This helped but something is still wrong. I Edited my question. – Jarne Teerlinck Mar 16 '15 at 10:56
  • @JarneTeerlinck It's better to ask it as a new question. Editing questions to add in solutions and more questions gets very confusing and makes it hard for people to follow. Ask your follow up questions in a new post and revert back to the old version of your question, linking the new question. – dav_i Mar 16 '15 at 11:00
  • @JarneTeerlinck There's a bit of a learning curve for Stack Overflow, but well worth it once you get it! – dav_i Mar 16 '15 at 11:05