【问题标题】:How to get parameters values in view component?如何获取视图组件中的参数值?
【发布时间】:2018-08-04 13:41:40
【问题描述】:

我已经创建了视图组件并向它发送了 2 个参数。

<vc:ModalComponent>
    title = "Create"
    id = "createModal"
</vc:ModalComponent>

如何从视图组件中获取titleid 值?

谢谢。

【问题讨论】:

  • 欢迎来到 StackOverflow。如果社区要帮助您,我们需要 a) 您期望看到的内容,b) 您正在看到的内容,c) 任何错误消息。请编辑问题以澄清这些要点。

标签: parameters asp.net-core-mvc asp.net-core-viewcomponent


【解决方案1】:

根据您的代码,我假设您定义了一个名为 ModalComponent 的 ViewComponent。

我建议您按照以下步骤操作:

  • ViewComponent 如下:

     public class ModalComponent : ViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync(
                string title, int id)
        {
            return View(new { Title = title, Id = id });
        }
     }
    
  • 在路径Views/&lt;controller_name&gt;/Components/&lt;view_component_name&gt;/&lt;view_name&gt;Views/Shared/Components/&lt;view_component_name&gt;/&lt;view_name&gt; 中创建视图

  • 调用视图组件

      <vc:modal-component title="title1"
                   id=2>
      </vc:modal-component>
    

注意

  • ModalComponent需要使用modal-component

  • 需要在&lt;vc:modal-component中设置带有属性的变量

【讨论】:

    【解决方案2】:

    您应该能够访问在其 Invoke 方法中发送到 ViewComponent 的数据。 例如

    public IViewComponentResult Invoke(string title, string id)
    {
        //do sth here
        return View();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 2012-12-11
      相关资源
      最近更新 更多