设置/更改控制器/视图组件中属性的显示(名称= " ")属性



根据2010年的这个答案,关于mvc-2,这是不可能的。现在,在 asp.net 核 2.2 中呢?

我的用例:

我有一个BaseViewModel被 2 个视图使用:TableView(适用于用户)和TableManagentView(适用于管理员)。BaseViewModelViewComponent调用。下面是一些示例:

基本视图模型:

public class BaseViewModel {
[Display(Name = "Comment")
public string UserComment { get; set; }
}

表视图:

@await Component.InvokeAsync(nameof(Base), new { myObject = myObject, stringName = "User"})

表管理视图:

@await Component.InvokeAsync(nameof(Base), new { myObject = myObject, stringName = "Admin"})

基础:

public class Base : ViewComponent
{
public IViewComponentResult Invoke(BaseViewModel myObjet, string invokingView)
{
// here i want to do something like that
if (invokingView == "User") {
myObject.UserComment.SetDisplayName("My Comment");
} 
if (invokingView == "Admin") {
myObject.UserComment.SetDisplayName("User's Comment");
}

return View("BaseViewComponent", myObject);
}
}

基本视图组件:

@Html.DisplayNameFor(model => model.UserComment)

BaseViewModel已简化,但还有更多属性。我想这样做的原因是为了避免两个表中的代码重复。唯一应该更改的是标签名称。

我试过reflection,但没有成功:

public IViewComponentResult Invoke(BaseViewModel myObject, string invokingView)
{
MemberInfo property = typeof(BaseViewModel).GetProperty("UserComment");
property.GetCustomAttributes(typeof(DisplayAttribute)).Cast<DisplayAttribute>().Single().Name = "test";
return View("BaseViewComponent", myObject);
}

Name不会更改,并且与初始设置保持"Comment"

如果无法以编程方式设置属性名称,我还有哪些其他解决方案?我正在考虑ViewBag/ViewDataTempData,但这个解决方案对我没有吸引力。这样做的利弊是什么?

扩展我留下的评论,解决这个问题的一种方法是让你的BaseViewModel成为一个抽象类,并从中派生出具体的类。所以UserViewModelAdminViewModel.然后,这两个具体类将成为TableViewTableManagentView的模型,并负责告诉"外部世界"如何标记字段。

基类有两个主要方面(除了普通字段):一个抽象Dictionary<string, string>,它将包含标签和一个从列表中获取标签的方法:string GetLabel(string propName).所以像这样:

public abstract class BaseViewModel
{
protected abstract Dictionary<string, string> Labels { get; }
public string UserComment { get; set; }
public string GetLabel(string propName)
{
if (!Labels.TryGetValue(propName, out var label))
throw new KeyNotFoundException($"Label not found for property name: {propName}");
return label;
}
}

然后创建两个派生类UserAdmin

public sealed class UserViewModel : BaseViewModel
{
protected override Dictionary<string, string> Labels => new Dictionary<string, string>
{
{ nameof(UserComment), "User label" }
};
}
public sealed class AdminViewModel : BaseViewModel
{
protected override Dictionary<string, string> Labels => new Dictionary<string, string>
{
{ nameof(UserComment), "Admin label" }
};
}

它们仅实现Dictionary<string, string>并为基类上的每个字段设置适当的文本。

接下来,将您的BaseViewComponent更改为以下内容:

查看

@model DisplayNameTest.Models.BaseViewModel
<h3>Hello from my View Component</h3>
<!-- Gets the label via the method on the base class -->
<p>@Model.GetLabel(nameof(BaseViewModel.UserComment))</p>
<p>@Model.UserComment)</p>

组件视图类(现在更简单)

public IViewComponentResult Invoke(BaseViewModel viewModel)
{
return View(viewModel);
}

最后,更改视图TableViewTableManagentView到:

@model WebApp.Models.AdminViewModel
@{
Layout = null;
}
<h1>Admin View</h1>
<div>
@await Component.InvokeAsync("Base", Model)
</div>

以及控制者:

public IActionResult Index()
{
var adminViewModel = new AdminViewModel { UserComment = "some comment from admin" };
return View(adminViewModel);
}

现在,当您导航到TableView时,您将向BaseViewComponent传递UserViewModel,它会找出正确的标签。引入新字段现在需要您更改视图模型,向字典添加新条目。

这并不完美,但我认为这是解决它的好方法。到目前为止,我不是MVC专家,所以也许其他人也可以想出一种更自然的方法。我还准备了一个工作示例应用程序并推送到 GitHub。你可以在这里查看:aspnet-view-component-demo。希望它以某种方式有所帮助。

最新更新