0

I got null reference exception when i trying to view data from table for actually logged user - Klient_has_sklep and if that user isnt signed with Sklep table. When i viewing data for user who is signed with Sklep table everything is ok.

I think problem is in Controller with viewModel

This is my controller

public ActionResult Index(int? id)
{
    var viewModel = new Klient_has_SklepIndexData();
    viewModel.Klients = db.Klients

  .OrderBy(i => i.Nazwisko);        
    UserManager UM = new UserManager();
    int idZalogowanego = UM.GetUserID(User.Identity.Name);
    ViewBag.idzal = idZalogowanego;

    viewModel.Klient_has_Skleps = viewModel.Klients.Where(i => i.SYSUserID == idZalogowanego).Single().Klient_has_Sklep;

    return View(viewModel);
}

This is my view

@model Sklepy.Models.ViewModel.Klient_has_SklepIndexData

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.idzal.</h2>
<h2>Twoje zniżki w sklepach</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>asd</th>
    </tr>


@if (Model != null)
{
    @foreach (var item in Model.Klient_has_Skleps)
    {
        <tr>
            <td>@item.Znizka1</td>

        </tr>
    }
}
</table>

This is my Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sklepy.Models.DB;

namespace Sklepy.Models.ViewModel
{
    public class Klient_has_SklepIndexData
    {
            public IEnumerable<Klient> Klients { get; set; }
            public IEnumerable<Klient_has_Sklep> Klient_has_Skleps {get; set;}
    }
}

Klient class code

 public partial class Klient
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Klient()
    {
        this.Klient_has_Sklep = new HashSet<Klient_has_Sklep>();
    }

    public int KlientID { get; set; }
    public int SYSUserID { get; set; }
    public string Imię { get; set; }
    public string Nazwisko { get; set; }
    public string Adres { get; set; }
    public string Telefon { get; set; }
    public string Email { get; set; }

    public virtual SYSUser SYSUser { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Klient_has_Sklep> Klient_has_Sklep { get; set; }
}
marylin17
  • 75
  • 1
  • 7

1 Answers1

1

Create your ViewModel as shown below. I made modification to make default initialization with List.

public class Klient_has_SklepIndexData
{
    public IList<Klient> Klients { get; set; }
    public IList<Klient_has_Sklep> Klient_has_Skleps { get; set; }

    public Klient_has_SklepIndexData()
    {
        Klients = new List<Klient>();
        Klient_has_Skleps = new List<Klient_has_Sklep>();
    }
}

And your Action Code.

    public ActionResult Index(int? id)
    {
        var viewModel = new Klient_has_SklepIndexData();
        viewModel.Klients = db.Klients
                            .OrderBy(i => i.Nazwisko).ToList();        
        UserManager UM = new UserManager();
        int idZalogowanego = UM.GetUserID(User.Identity.Name);
        ViewBag.idzal = idZalogowanego;

        var skelp = viewModel.Klients.FirstOrDefault(i => i.SYSUserID == idZalogowanego);

        if(skelp != null){
            if(skelp.Klient_has_Sklep != null){
                viewModel.Klient_has_Skleps = skelp.Klient_has_Sklep.ToList();
            }
        }

        return View(viewModel);
    }
ramiramilu
  • 17,044
  • 6
  • 49
  • 66
  • I used copy-paste method. Still error `Cannot convert from 'System.Collections.Generic.ICollection' to 'Sklepy.Models.DB.Klient_has_Sklep'` – marylin17 Jan 09 '16 at 17:37
  • I have database first, so it looks like this: [link](http://s12.postimg.org/l8gqsoq3x/cap1.png) – marylin17 Jan 09 '16 at 17:49
  • And this line return error: `Only assignment, call, increment, decrement, and new object expressions can be used as a statement` Edited my post, added Klient class code. – marylin17 Jan 09 '16 at 17:51
  • Still doesn't work In the same line: `Cannot implicitly convert type 'System.Collections.Generic.ICollection' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)` – marylin17 Jan 09 '16 at 18:06
  • `System.InvalidOperationException` in same line. – marylin17 Jan 09 '16 at 18:17
  • Ehm, I think there is lack of something if I use `viewModel.Klient_has_Skleps = viewModel.Klients.Single(i => i.SYSUserID == 5002).Klient_has_Sklep.ToList();` instead of `viewModel.Klient_has_Skleps = viewModel.Klients.Single(i => i.SYSUserID == idZalogowanego).Klient_has_Sklep.ToList();` everything looks works ok. But if Klient isnt related with Sklep in Klient_has_Sklep I got error ps. Klient with `SysuserID ==5002` is related with two Sklep (shops). ps2. It works like as the beginning – marylin17 Jan 09 '16 at 18:33
  • Use FirstOrDefault - `var skelp = viewModel.Klients.FirstOrDefault(i => i.SYSUserID == idZalogowanego)`, then check whether `skelp` for null, then use that object again to check for null - `skelp.Klient_has_Sklep`. then finally use - `skelp.Klient_has_Sklep.ToList()` – ramiramilu Jan 09 '16 at 18:42
  • ha! it is working well now, I must read and understand that code now and make some additional changes, because it's not everything – marylin17 Jan 09 '16 at 18:55