Below is an abstract of my Controller and View. I would like to call the method GetUserDistinguished from the View. This is only cosmetic. The Guid is ugly and doesn’t make sense to the end user, but it’s the unique ID I am using for users.
namespace Users.Controllers
{
public class UsersController : Controller
{
public ViewResult Index()
{
var users = db.Users.Include("Users");
return View(users.ToList());
}
public string GetUserDistinguished(string Guid)
{
return ADHelper.ConvertGuidToDn(Guid);
}
}
}
@model IEnumerable<Test.GuidToDistinguishedName>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Name from Guid
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Guid)
</td>
</tr>
}
</table>
Depending on what you want to achieve, there are two approaches you can take:
If you want to display pairs of Guids and Distinguished names on the page you should just set a collection of such pairs as a model for the view.
If you want to show a distinguished name to the user when he clicks somewhere, you should add an action that accepts the guid and sets its distinguished name along with other required data as a model for another view.
Looking at your sample code I’m not sure what your scenario is.
EDIT:
Based on your comment, the first approach is the right one: you need to prepare all the data for the view in your controller and put it in the model.
You can modify your
Indexmethod like this to set up pairs of guids and distinguished names (of course you can always use your own custom class as a model if you need to pass more data – that’s even encouraged):In the view you can then iterate over the pairs to display them in the table:
Don’t forget to set the proper model type at the top of the page, in my case: