ASP.NET Core 5 MVC 从视图中的动作渲染字符串
ASP.NET Core 5 MVC Render string from action in view
我尝试在视图中显示 Action 中的字符串。这听起来像是一项非常简单易行的任务,但我不知道该怎么做。
我的异步操作returns内容结果:
public async Task<IActionResult> DisplayName(string key)
{
// Retrieves the requested culture
var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
// Culture contains the information of the requested culture
var culture = rqf.RequestCulture.Culture;
return Content(await Task.FromResult(_displayProvider.GetDisplayName(key, culture.Name)));
}
我的观点很简单html:
@model List<MyModel>
<table class="table table-bordered table-striped datatable">
<thead class="table-head-dark">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().Id)
</th>
<th>
Actions
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
<partial name="_EditDetailButton" model="@item.Id" />
<partial name="_DeleteButton" model="@item" />
</td>
</tr>
}
</tbody>
</table>
而不是 @Html.DisplayNameFor(model => model.First().Id)
我想显示一个 user-defined 值。 user-defined 值从操作返回。
我试过 @Url.Action("DisplayName", "DisplayName", new { key = "MyModel.Id" })
但这是渲染 Url。
我尝试了 Html.RenderAction("DisplayName", "DisplayName", new { key = "LastName" })
但 ASP.NET Core 5 中不存在 RenderAction。
我可以调用静态 Class e。 G。 DisplayNameProvider.GetDisplayName("MyModel.Id", ???)
但我不知道如何让选择的文化将其传递给方法。
我如何让它工作?我也不熟悉 ASP.NET Core 中的组件。
或者是否有完全不同的显示字符串的解决方案,用户已定义并保存到数据库? DisplayNameProvider
正在从数据库中检索字符串并处理缓存。
更新:
目标是存储和更改数据库中属性的显示名称。用户可以更改显示名称。在我的示例中,用户想要列 header 的不同显示名称。我不能使用资源文件,因为它们是静态的,无法在运行时更改。
这是我的DisplayNameProvider.GetDisplayName方法:
public string GetDisplayName(string ressourceKey, string language)
{
var ressources = GetCached(language);
var item = ressources.FirstOrDefault(r => r.ResourceKey == ressourceKey);
return item.Text;
}
提前致谢
我最终使用了一个字符串本地化器,它 returns 我的属性所需的值。
现在我可以使用 displaynamefor 了,它显示了语言的价值。例如:
@Html.DisplayNameFor(model => model.MyProperty)
我尝试在视图中显示 Action 中的字符串。这听起来像是一项非常简单易行的任务,但我不知道该怎么做。
我的异步操作returns内容结果:
public async Task<IActionResult> DisplayName(string key)
{
// Retrieves the requested culture
var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
// Culture contains the information of the requested culture
var culture = rqf.RequestCulture.Culture;
return Content(await Task.FromResult(_displayProvider.GetDisplayName(key, culture.Name)));
}
我的观点很简单html:
@model List<MyModel>
<table class="table table-bordered table-striped datatable">
<thead class="table-head-dark">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().Id)
</th>
<th>
Actions
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
<partial name="_EditDetailButton" model="@item.Id" />
<partial name="_DeleteButton" model="@item" />
</td>
</tr>
}
</tbody>
</table>
而不是 @Html.DisplayNameFor(model => model.First().Id)
我想显示一个 user-defined 值。 user-defined 值从操作返回。
我试过 @Url.Action("DisplayName", "DisplayName", new { key = "MyModel.Id" })
但这是渲染 Url。
我尝试了 Html.RenderAction("DisplayName", "DisplayName", new { key = "LastName" })
但 ASP.NET Core 5 中不存在 RenderAction。
我可以调用静态 Class e。 G。 DisplayNameProvider.GetDisplayName("MyModel.Id", ???)
但我不知道如何让选择的文化将其传递给方法。
我如何让它工作?我也不熟悉 ASP.NET Core 中的组件。
或者是否有完全不同的显示字符串的解决方案,用户已定义并保存到数据库? DisplayNameProvider
正在从数据库中检索字符串并处理缓存。
更新: 目标是存储和更改数据库中属性的显示名称。用户可以更改显示名称。在我的示例中,用户想要列 header 的不同显示名称。我不能使用资源文件,因为它们是静态的,无法在运行时更改。
这是我的DisplayNameProvider.GetDisplayName方法:
public string GetDisplayName(string ressourceKey, string language)
{
var ressources = GetCached(language);
var item = ressources.FirstOrDefault(r => r.ResourceKey == ressourceKey);
return item.Text;
}
提前致谢
我最终使用了一个字符串本地化器,它 returns 我的属性所需的值。
现在我可以使用 displaynamefor 了,它显示了语言的价值。例如:
@Html.DisplayNameFor(model => model.MyProperty)