根据this对2010年的回答,关于mvc-2,这是不可能的。那么现在,在asp.net-core 2.2中呢?
我的用例:
我有一个BaseViewModel
被2个视图使用:TableView
(对于用户)和TableManagentView
(对于管理员)。 BaseViewModel
是由ViewComponent
引用的。以下是一些示例:
BaseViewModel:
public class BaseViewModel {
[Display(Name = "Comment")
public string UserComment { get; set; }
}
TableView中:
@await Component.InvokeAsync(nameof(Base), new { myObject = myObject, stringName = "User"})
TableManagementView:
@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);
}
}
BaseViewComponent:
@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/ViewData
或TempData
,但这个解决方案并不适合我。什么是赞成者和骗子?
扩展我留下的评论,你可以解决这个问题的一种方法是让你的BaseViewModel
成为一个抽象类,并从中得到具体的类。所以UserViewModel
和AdminViewModel
。这两个具体的类将成为TableView
和TableManagentView
的模型,并将负责告诉“外部世界”如何标记字段。
基类有两个主要方面(除了你的普通字段):一个抽象的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;
}
}
然后你创建两个派生类User
和Admin
:
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>
ComponentView类(现在更简单)
public IViewComponentResult Invoke(BaseViewModel viewModel)
{
return View(viewModel);
}
最后,将您的观点TableView
和TableManagentView
更改为:
@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
时,你会将UserViewModel
传递给BaseViewComponent
,它会找出正确的标签。现在,引入新字段需要您更改视图模型,向Dictionary添加新条目。
它并不完美,但我认为这是解决问题的好方法。我到目前为止还不是MVC专家,所以也许其他人也可以提出一种更自然的方式来做到这一点。我还准备了一个工作示例应用程序并推送到GitHub。你可以在这里查看:aspnet-view-component-demo。希望它能以某种方式帮助。