如何在多语言中使用 script type="text/x-template"?

问题描述

我将 ASP NET 与 VueJS 一起使用。 ASP NET 渲染视图组件:

<script type="text/x-template" id="form-template">
    @if (string.IsNullOrEmpty(Model.LastName))
    {
        <input asp-for="LastName" class="form-control" maxlength="100" autofocus required />
    }
    else
    {
        <input asp-for="LastName" class="form-control" maxlength="100" required />
    }
</script>

我对 C# 的操作:

    public async Task<ActionResult<ViewComponent>> GetAdditionalFormRegistrationComponentAsync()
    {
            var customerviewmodel = new CustomerAdditionFormviewmodel
            {                    
                LastName = "русские символы"
            };

            return ViewComponent("FormRegistration",customerviewmodel);
        }
    }

视图组件:

    public Task<IViewComponentResult> InvokeAsync(CustomerAdditionFormviewmodel customerviewmodel)
    {
        return Task.Run<IViewComponentResult>(() => View(customerviewmodel));
    }

姓氏写英文输入值OK。但如果姓氏填写俄语,我会得到:

enter image description here

如何解决这个问题?

解决方法

我找到了解决办法。 需要添加 Html.Raw()

<input value=“@Html.Raw(Model.LastName)" class="form-control" maxlength="100" autofocus required />

services.AddWebEncoders(options =>
{
    options.TextEncoderSettings = new TextEncoderSettings();
    options.TextEncoderSettings.AllowRange(UnicodeRanges.All);
});