Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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 core .Net Core-将id传递给部分视图,部分视图需要从id获取更多数据并显示结果_Asp.net Core_Razor_Entity Framework Core - Fatal编程技术网

Asp.net core .Net Core-将id传递给部分视图,部分视图需要从id获取更多数据并显示结果

Asp.net core .Net Core-将id传递给部分视图,部分视图需要从id获取更多数据并显示结果,asp.net-core,razor,entity-framework-core,Asp.net Core,Razor,Entity Framework Core,我希望局部视图根据传递的值获取更多数据:id 这就是我将值传递给局部视图的方式: @Html.Partial(“\u Customer”,“123”) 从\u Customer部分视图,我希望它能够访问我创建的sql server视图 +-----+------+-----+ | Id | Name | Age | +-----+------+-----+ | 122 | Jim | 35 | +-----+------+-----+ | 123 | Elon | 47 | +-----

我希望局部视图根据传递的值获取更多数据:
id

这就是我将值传递给局部视图的方式:
@Html.Partial(“\u Customer”,“123”)

\u Customer
部分视图,我希望它能够访问我创建的sql server视图

+-----+------+-----+
| Id  | Name | Age |
+-----+------+-----+
| 122 | Jim  | 35  |
+-----+------+-----+
| 123 | Elon | 47  |
+-----+------+-----+
抓取名称和年龄,并将其显示在部分视图输入框中


想知道是否有一种简单的方法可以做到这一点吗?

例如,您可以编写一个服务并在部分视图中进行注入-

服务

public interface IUserInfo
    {
        User GetUserInforById(int id);
    }

    public class UserInfo : IUserInfo
    {
        private readonly MyDbContext _cotext;

        // Inject DbContext 
        public UserInfo(MyDbContext dbContext)
        {
            _cotext = dbContext;
        }
        public User GetUserInforById(int id)
        {
            return _cotext.Users.Single(i => i.Id == id);
        }
    }
局部视图

@inject SeedSample.Services.IUserInfo UserInfo

@{ 
    var information = UserInfo.GetUserInforById(Model.Id);
}

@*write your html code*@

例如,您可以编写服务并在部分视图中注入-

服务

public interface IUserInfo
    {
        User GetUserInforById(int id);
    }

    public class UserInfo : IUserInfo
    {
        private readonly MyDbContext _cotext;

        // Inject DbContext 
        public UserInfo(MyDbContext dbContext)
        {
            _cotext = dbContext;
        }
        public User GetUserInforById(int id)
        {
            return _cotext.Users.Single(i => i.Id == id);
        }
    }
局部视图

@inject SeedSample.Services.IUserInfo UserInfo

@{ 
    var information = UserInfo.GetUserInforById(Model.Id);
}

@*write your html code*@

视图组件类似于局部视图,但功能更强大。视图组件不使用模型绑定,只依赖于调用时提供的数据

请参阅以下步骤:

1.在根路径中创建的
ViewComponents
文件夹中创建
CustomerDetailsViewComponent

    public class CustomerDetailsViewComponent:ViewComponent

    {

       private readonly MVCDbContext _context;

       public CustomerDetailsViewComponent(MVCDbContext context)
       {
          _context = context;
       }

       public async Task<IViewComponentResult> InvokeAsync(int customerId)
       {
          var model = await _context.Customer.FindAsync(customerId);
          return View(model);
       }
    }
3.要使用视图组件,请在视图内调用以下命令:

@await Component.InvokeAsync("CustomerDetails", new { customerId= 123})

有关视图组件的更多详细信息,请参阅此

视图组件与局部视图类似,但功能更强大。视图组件不使用模型绑定,只依赖于调用时提供的数据

请参阅以下步骤:

1.在根路径中创建的
ViewComponents
文件夹中创建
CustomerDetailsViewComponent

    public class CustomerDetailsViewComponent:ViewComponent

    {

       private readonly MVCDbContext _context;

       public CustomerDetailsViewComponent(MVCDbContext context)
       {
          _context = context;
       }

       public async Task<IViewComponentResult> InvokeAsync(int customerId)
       {
          var model = await _context.Customer.FindAsync(customerId);
          return View(model);
       }
    }
3.要使用视图组件,请在视图内调用以下命令:

@await Component.InvokeAsync("CustomerDetails", new { customerId= 123})

有关视图组件的更多详细信息,请参考此

如果我想在telerik下拉列表中输入年龄或姓名值,它会是这样吗
.value(TestProject.Model.Customer.Age)
?你到底是什么意思?根据您最初的需求,根据传递的id获取更多数据,结果是单个数据而不是集合。为什么要将它们放在下拉列表中?假设我有第三列
客户类型
,有一个
客户类型
的下拉列表,Elon有
客户类型ID=1
。。我如何将其填入telerik的下拉列表值?(这基本上是在下拉列表中进行选择)如果我想在telerik下拉列表中输入年龄或姓名值,会是这样吗
.value(TestProject.Model.Customer.Age)
?你到底是什么意思?根据您最初的需求,根据传递的id获取更多数据,结果是单个数据而不是集合。为什么要将它们放在下拉列表中?假设我有第三列
客户类型
,有一个
客户类型
的下拉列表,Elon有
客户类型ID=1
。。我如何将其填入telerik的下拉列表值?(这基本上在下拉列表中进行选择)。基本上我只需在html中输入
@information.age
&
@information.name
?是的,例如age:@information.age基本上我只需在html中输入
@information.age
&
@information.name
?是的,例如age:@information.age