Ber53rker
Ber53rker

Reputation: 649

Get Name from ID via Active Directory Method?

Kind of hard for me to explain., but I'll try.

AssignedTo is a Guid in the database. I need to compare it to a list of Guids in the AD to get a name to show.

I have no problem doing the AD stuff. It's more of the how I do it and display the name in the view that's problematic.

Thanks for the help.

_

View: (Model is Entity Model of DB)

    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.AssignedTo</td>
            </tr>
        }
    </tbody>

Controller:

    private MyEntities db = new MyEntities();
    public ViewResult Index()
    {
        return View(db.Items.ToList());
    }

AD Code: (Not sure where to put this and make it accessible to the view.)

    public string GetUserFromGuid(Guid g)
    {
        ActiveDirectoryModel adm = new ActiveDirectoryModel();
        return adm.FetchContacts().Find(n => n.Value == g.ToString()).Text;
    }

Upvotes: 0

Views: 287

Answers (2)

Ber53rker
Ber53rker

Reputation: 649

I fixed the issue by creating a partial class.

public partial class Item
{
    public string AssignedToName { get; set; }
}

And then doing something close to what Dimitri suggested...

model.ForEach(x => x.AssignedToName = GetUserFromGuid(x.AssignedTo));

And then displaying it...

<td>@item.AssignedToName</td>

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

Adapt the view model you are passing to your view:

private MyEntities db = new MyEntities();

public ViewResult Index()
{
    var model = db.Items.ToList().Select(x => GetUserFromGuid(x.AssignedTo));
    return View(model);
}

and then in your view:

@model IEnumerable<string>
...
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item</td>
        </tr>
    }
</tbody>

Upvotes: 1

Related Questions