如何解决ASP.NET Core:将存储过程的结果获取到视图模型
我有一个名为TdetailsVM的视图模型,如下所示:
public class TdetailsVM
{
public Tournaments tourney { get; set; }
public List<Participants> currentlyjoined { get; set; }
}
现在在控制器中,我正在将比赛的ID传递给任务:
public async Task<IactionResult> Details(guid id)
{
var ThisTourney = _context.Tournaments.FirstOrDefaultAsync(m => m.TID == id);
这会将特定比赛的值返回到ThisTourney
中,稍后我将其传递给视图模型
我需要类似的东西:
var ThisParticipants = (result "has many rows" from a stored procedure called SP_GetParticipants that needs a parameter =id)
然后我可以将这些值传递给视图模型,如下所示
TdetailsVM tvm = new TdetailsVM()
{
tourney = ThisTourney,currentlyjoined = ThisParticipants
}
// then I can return the view
return view(tvm);
传递第一个要求的数据很好并且可以,但是如何传递存储过程?
非常感谢
解决方法
如果您使用的是Entity Framework Core,则可以使用以下代码行来调用存储过程。
List<Participants> participants= _context.Participants.FromSql("SP_GetParticipants @ID",id).ToList();
请注意如何将
@ID
的值传递给FromSql
中的存储过程 方法以及如何将结果映射到List<Participants>
。有关更多信息,请查看raw sql。
在这行代码之后,您列出了Participants
,然后可以填充父级ViewModel。
var result = new TdetailsVM
{
Tourney = thisTourney,Currentlyjoined = Participants
};
请注意,建议使用
PascalCase
来命名您的公众 类(ViewModel)和camelCase
中的属性 私人的。请看一下General Naming Conventions 有关更多信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。