简体   繁体   中英

Fetch data from a List and display in View

I create a small ASP.NET CORE MVC which fetch User from ActiveDirectory. RIght now I need to display these data in View but I dont know how.

 public string VratiKorisnike()
        {
            List<Korisnik> lstuser = new List<Korisnik>();
            string sDomainName = "stesthu";
            string DomainPath = "LDAP://" + sDomainName;

            DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
            DirectorySearcher dssearch = new DirectorySearcher("LDAP://" + rootDSE);
            dssearch.Filter = "(&(objectClass=user)(objectCategory=person))";
            dssearch.PropertiesToLoad.Add("Ime");
            dssearch.PropertiesToLoad.Add("Prezime");
            dssearch.PropertiesToLoad.Add("LoginName");
            dssearch.PropertiesToLoad.Add("Status");
            dssearch.PropertiesToLoad.Add("AccountExpired");
            dssearch.PropertiesToLoad.Add("PassNevExp");
            dssearch.PropertiesToLoad.Add("DomenskaGrupa");
            dssearch.PropertiesToLoad.Add("Email");

            DataTable dt = new DataTable();
            dt.Columns.Add("Ime");
            dt.Columns.Add("Prezime");
            dt.Columns.Add("LoginName");
            dt.Columns.Add("Status");
            dt.Columns.Add("AccountExpired");
            dt.Columns.Add("PassNevExp");
            dt.Columns.Add("DomenskaGrupa");
            dt.Columns.Add("Email");

            SearchResult sresult = dssearch.FindOne();
            //int i = 0;

            SearchResultCollection resultCol = dssearch.FindAll();

            if (resultCol != null)
            {
                int counter;
                for (counter = 0; resultCol.Count < 0; counter++)
                {
                    //string UserNameEmail;

                    if (resultCol.PropertiesLoaded.Contains("Ime") || resultCol.PropertiesLoaded.Contains("Prezime"))
                    {
                        //List<Korisnik> korisnik = new List<Korisnik>();
                        Korisnik korisnik = new Korisnik();

                        var Ime = sresult.Properties["Ime"][0].ToString();
                        var Prezime = sresult.Properties["Prezime"][0].ToString();
                        var LoginName = sresult.Properties["LoginName"][0].ToString();
                        var Status = sresult.Properties["Status"][0].ToString();
                        var AccountExpired = sresult.Properties["AccountExpired"][0].ToString();
                        var PassNevExp = sresult.Properties["PassNevExp"][0].ToString();
                        var DomenskaGrupa = sresult.Properties["DomenskaGrupa"][0].ToString();
                        var Email = sresult.Properties["Email"][0].ToString();

                        DataRow dr = dt.NewRow();
                        dr["Ime"] = korisnik.Ime;
                        dr["Prezime"] = korisnik.Prezime;
                        dr["LoginName"] = korisnik.LoginName;
                        dr["Status"] = korisnik.Status;
                        dr["AccountExpired"] = korisnik.AccountExpired;
                        dr["PassNevExp"] = korisnik.PassNevExp;
                        dr["DomenskaGrupa"] = korisnik.DomenskaGrupa;
                        dr["Email"] = korisnik.Email;

                        dt.Rows.Add(dr);
                        lstuser.Add(korisnik);

                    }

                }
            }

            return "";

}

And in my controller I call VratiKorisnike()

public IActionResult VratiKorisnike()

    {
        ViewBag.Korisnici = _korisnikServis.VratiKorisnike();            
        return View("Index"); 

}

And I use ViewBag but there is no data display in View

@foreach (var item in ViewBag.Korisnici)
{
    <table>
        <tr>
            <th>Ime</th>
            <th>Prezime</th>
        </tr>
        <tr>
            <td>@Model.Ime</td>
            <td>@Model.Prezime</td>
        </tr>
    </table>
}

Firstly you need to return your lstuser , so what you need to do is to change your method's return type to List<Korisnik> first and then return the lstuser inside your method, so change this:

public string VratiKorisnike()

To this:

public List<Korisnik> VratiKorisnike()

And add this:

return lstuser;

What you need to do secondly, is casting your ViewBag.Korisnici to List<Korisnik> in your view and use item variable instead of Model in the foreach loop:

    @foreach (var item in (List<Korisnik>)ViewBag.Korisnici)
{
    <table>
        <tr>
            <th>Ime</th>
            <th>Prezime</th>
        </tr>
        <tr>
            <td>@item.Ime</td>
            <td>@item.Prezime</td>
        </tr>
    </table>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM