MVC 3:使用 EditorForModel 隐藏 ID 属性
我的视图中有这一行:
@Html.EditorForModel()
这是我的 ViewModel:
public class CommentForm
{
public int Id { get; set; }
[DisplayName("Kommentar"), DataType(DataType.MultilineText)]
public string Comment { get; set; }
}
问题是 Id
在表单中呈现为文本字段。实际上,我只想在表单操作中使用 Id
。是否有一个属性告诉编辑器不要渲染属性Id
?
I have this line in my View:
@Html.EditorForModel()
And this is my ViewModel:
public class CommentForm
{
public int Id { get; set; }
[DisplayName("Kommentar"), DataType(DataType.MultilineText)]
public string Comment { get; set; }
}
The problem is that Id
renders as a textfield in the form. Actually, I only want to use Id
in the form action. Is there an attribute that tells the editor not to render the property Id
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准属性已将 ShowForDisplay 和 ShowForEdit 设置为 false
。
因此,您的自定义属性似乎有点矫枉过正。
Setting ShowForDisplay and ShowForEdit to false is already done by the standard
attribute.
Your custom attribute therefore seems like overkill.
一种可能性是将其呈现为隐藏字段:
另一种可能性是为您的
CommentForm
视图模型编写自定义编辑器模板,并在该模板中包含您想要的任何内容(~/Views/Shared/ EditorTemplates/CommentForm.cshtml
):One possibility is to render it as a hidden field:
Another possibility is to write a custom editor template for your
CommentForm
view model and inside this template include whatever you want (~/Views/Shared/EditorTemplates/CommentForm.cshtml
):感谢您的贡献,但我不太喜欢它们。
我制作了自己的 PreventRenderingAttribute。
PreventRenderingAttribute.cs
并在 CommentForm 中
Thanks for your contributions, but I didn't really like them.
I made my own PreventRenderingAttribute.
PreventRenderingAttribute.cs
And in CommentForm