是否可以将ModelExpression传递给TagHelper? [英] Is it possible to pass ModelExpression to a TagHelper?

查看:250
本文介绍了是否可以将ModelExpression传递给TagHelper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用TagHelper中的此属性,我们可以获得 ModelExpression

We can get a ModelExpression using this property in a TagHelper:

[HtmlAttributeName("asp-for")]
public ModelExpression For { get; set; }

我设法以某种方式使ViewModel拥有一个 ModelExpression 属性:

I somehow managed* to have a ViewModel wich has a ModelExpression property:

public class TemplateViewModel
{
    public ModelExpression For { get; set; }
}

每次我尝试传递它时,模型表达式都是<$ c $来自 TemplateViewModel 的c> For ,不是真正的Expression wich存储在 For 中:

Everytime I try to pass it, the Model expression is For from TemplateViewModel, not the real Expression wich is stored into For:

@model TemplateViewModel
<input asp-for="@Model.For" class="form-control"/>

以上结果:

<input class="form-control" type="text" id="For" name="For" value="Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpression" />

我期望输入,由 ModelExpression 代替 ModelExpression ModelExpression

I expected, the input, which is described by the ModelExpression instead of literaly a ModelExpression for the ModelExpression.

*因为我想使用 IHtmlHelper :: PartialView()获得TagHelper的模板视图。此示例已大大减少。我的主要动机是为单个< form-group for = /> TagHelper,它生成一个引导表单组

*since I want to have a Template View for an TagHelper using IHtmlHelper::PartialView(). This example is heavy minimized. My main motiviation is to create a single <form-group for="" /> TagHelper, which is generating a Bootstrap Form Group.

推荐答案

ModelExpression Razor编译器,因此无法直接使用。由于编译器是开源的,因此您可以建议使用一个补丁来忽略属性本身为 ModelExpression 的情况。同时,您将需要在代码帮助器中使用其他类型的属性,以帮助您引用实际的 ModelExpression 。也许 public Func< ModelExpression> ForAccessor {获取;组; }

ModelExpression is handled as a special case in the Razor Compiler, so this isn't going to work directly. Since the compiler is open source, you could suggest a patch to ignore cases where the property is itself a ModelExpression. In the mean time, you're going to need to use a different type of property in your tag helper, to help you get a reference to the actual ModelExpression. Perhaps public Func<ModelExpression> ForAccessor { get; set; }?

这篇关于是否可以将ModelExpression传递给TagHelper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆