Reputation: 17131
View:
@model dynamic
<div class="btn-group">
<a class="btn btn-default dropdown-toggle btn-select" data-toggle="dropdown" href="#">Select a Country <span class="caret"></span></a>
<ul class="dropdown-menu">
@foreach (dynamic m in Model)
{
<li><a href="#">"@m.EmployeeName"</a></li>
}
</ul>
</div>
Controller :
public ActionResult Index()
{
var Employees = Connections.SaleBranch.SqlConn.Query("SELECT EmployeeName,EmployeeID FROM dbo.udft_Employee(@RunDate) WHERE OfficerEmployeeID=@OfficerEmployeeID",
new { OfficerEmployeeID = 78273, RunDate = DateTime.Now.GetPersianDate() },
commandType: CommandType.Text).ToList();
var EmployeesList = Employees.Select(x => new { EmployeeName = x.EmployeeName, EmployeeID = x.EmployeeID }).ToList();
return View("Point/Index", EmployeesList);
}
The object m
shows 2 properties(EmployeeName,EmployeeID).
But can't fetch m.EmployeeName
value
Upvotes: 2
Views: 8430
Reputation: 13498
Try get desired value via reflection:
@foreach (dynamic m in Model)
{
var EmployeeName = m.GetType().GetProperty("EmployeeName").GetValue(m);
<li><a href="#">"@EmployeeName"</a></li>
}
Upvotes: 6
Reputation:
That's because the objects in employeesList are not dynamic but are anonymous objects. Anonymous can't be used outside the scope they are created in.
A dynamic view model is not a good idea, but if you insist, you can look here. Instead I would make a strongly typed model for the view.
Upvotes: 2