2013-12-13 77 views
9

虽然嘟嘟尖说,只读属性在MVC 4

enter image description here

我试图使用它,但不能使它工作。我不确定它是如何工作的以及它的功能。 那么这个内置属性ReadOnly有什么用途?

任何输入将不胜感激。谢谢。

回答

12

我假设你在视图中使用这个属性,类似EditorFor?然后使用:

[Editable(false)] 
public string MyProperty {get;set;} 

@Html.TextBoxFor(x => x.MyProperty, new { readonly = "readonly" }) 

如果你想有一个类使用的只读公共财产:

public string MyProperty {get; private set;} 
+0

谢谢你的职位。我明白了你的观点,但我很好奇的是在模型属性上使用了这个属性。 –

+0

我认为readonly属性只是用于反射的元数据,而不是MVC EditorFor。更多信息在这里:http://msdn.microsoft.com/en-us/library/system.componentmodel.readonlyattribute.aspx这是一个工作只读属性的解决方案:http://stackoverflow.com/questions/18515441/mvc -4-razor-data-annotations-readonly – Marthijn

+1

@ Html.TextBoxFor(x => x.MyProperty,new {@readonly = true})这也在工作 – unique