Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何使用桥接表实体框架获取用户ID_Asp.net Mvc_Entity Framework_Razor - Fatal编程技术网

Asp.net mvc 如何使用桥接表实体框架获取用户ID

Asp.net mvc 如何使用桥接表实体框架获取用户ID,asp.net-mvc,entity-framework,razor,Asp.net Mvc,Entity Framework,Razor,我在一个项目中工作,我想在让他更新或删除之前检查用户id。为此,我使用实体框架和ASP.NETMVC 表1是 public class User { public int UserId { get; set; } // more stuff here. public ICollection<UserPost> UserPost{get; set;} } 在我看来,我想检查用户更新或删除他的文章,像这样的代码,但它不工作 @model List<

我在一个项目中工作,我想在让他更新或删除之前检查用户id。为此,我使用实体框架和ASP.NETMVC

表1是

public class User {
    public int UserId { get; set; }  

    // more stuff here. 

    public ICollection<UserPost> UserPost{get; set;}
}
在我看来,我想检查用户更新或删除他的文章,像这样的代码,但它不工作

@model List<MyFinalProject.Core.Domain.Post>
// Razor syntax here 
    @if (User.Identity.IsAuthenticated && User.IsInRole("Admin") ||
                            Model[i].UserPost.Where(u => u.UserId == User.Identity.GetUserId<int>()))
                        { // some logic } 
@型号列表
//Razor语法在这里
@if(User.Identity.IsAuthenticated&&User.IsInRole(“管理员”)||
模型[i].UserPost.Where(u=>u.UserId==User.Identity.GetUserId())
{//一些逻辑}

您是否已将用户的身份服务添加到Startup.cs的ConfigureServices()中

有关AspNet.Identity的更多信息,请查看:

非常感谢您的帮助,我刚刚找到了答案,我想与您分享。我仅将(| |)条件后的第二部分更改为:

@if (User.Identity.IsAuthenticated && User.IsInRole("Admin") ||
                     Model[i].UserPost.Any(e => e.UserId == User.Identity.GetUserId<int>()))
@if(User.Identity.IsAuthenticated&&User.IsInRole(“Admin”)||
模型[i].UserPost.Any(e=>e.UserId==User.Identity.GetUserId())

只需检查条件if…的第一部分,然后测试第二部分(在'or'| |之后),即可尝试对视图进行故障排除。我想问题出在逻辑的第二部分。如果是这样,请避免使用linq逻辑。而是:Model[i].UserPost.UserId==User.Identity.GetUserID()或类似的东西……希望这能有所帮助。是的,我的问题是在(| |)我尝试使用Model[i].UserPost.UserId后,条件的第二部分出现了,但我无法访问它@SRQCoder
@model List<MyFinalProject.Core.Domain.Post>
// Razor syntax here 
    @if (User.Identity.IsAuthenticated && User.IsInRole("Admin") ||
                            Model[i].UserPost.Where(u => u.UserId == User.Identity.GetUserId<int>()))
                        { // some logic } 
@if (User.Identity.IsAuthenticated && User.IsInRole("Admin") ||
                     Model[i].UserPost.Any(e => e.UserId == User.Identity.GetUserId<int>()))