gpt4 book ai didi

c# - 如何从 MVC razor c# 中的动态模型获取属性值

转载 作者:行者123 更新时间:2023-12-05 09:19:33 25 4
gpt4 key购买 nike

查看:

@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);
}

对象 m 显示 2 个属性(EmployeeName、EmployeeID)。

enter image description here

但无法获取m.EmployeeName

enter image description here

最佳答案

尝试通过反射获得所需的值:

@foreach (dynamic m in Model)
{
var EmployeeName = m.GetType().GetProperty("EmployeeName").GetValue(m);
<li><a href="#">"@EmployeeName"</a></li>
}

关于c# - 如何从 MVC razor c# 中的动态模型获取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40259878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com