我不知道您是否曾经想出解决方案,如果您这样做了,我很想知道,但对于其他偶然发现这个问题的人来说,我最终做的是传递所有人的名字列表用户对视图模型具有只读访问权限的字段,然后在我的 cshtml 页面中检查该字段是否是该列表的一部分,并基于此禁用输入标记。
public class DataFields{
public string Field1 {get; set;}
public string Field2 {get; set;}
public string Field3 {get; set;}
public string Field4 {get; set;}
public string Field5 {get; set;}
}
public class DataFieldsViewModel{
public DataFields DataFields {get; set;}
public IEnumerable<string> ReadOnlyFields {get; set;}
}
然后在我的cshtml页面上是这样的:
@var isField1ReadOnly = Model.ReadOnlyFields.Contains(nameof(Model.DataFields.Field1));
@var isField2ReadOnly = Model.ReadOnlyFields.Contains(nameof(Model.DataFields.Field2));
@var isField3ReadOnly = Model.ReadOnlyFields.Contains(nameof(Model.DataFields.Field3));
@var isField4ReadOnly = Model.ReadOnlyFields.Contains(nameof(Model.DataFields.Field4));
@var isField5ReadOnly = Model.ReadOnlyFields.Contains(nameof(Model.DataFields.Field5));
...
<input asp-is-disabled="@(isField1ReadOnly)" asp-for"Model.DataFields.Field1">
<input asp-is-disabled="@(isField2ReadOnly)" asp-for"Model.DataFields.Field2">
asp-is-disabled是基于这样的:https ://stackoverflow.com/a/34868369/4573789