Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReadOnly Attribute in MVC 4

While the toot-tip says,

enter image description here

I tried using it but could not make it to work. I am not sure how it works and about its functionality. So what purpose does this built-in attribute ReadOnlyserve?

Any inputs will be appreciated. Thank you.

like image 285
Aritra B Avatar asked Dec 13 '13 12:12

Aritra B


2 Answers

I assume you use this property in a view with something like EditorFor? Then use:

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

or

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

If you want a readonly public property of a class use:

public string MyProperty {get; private set;}
like image 94
Marthijn Avatar answered Sep 19 '22 18:09

Marthijn


@Html.EditorFor(
  model => model.id_to_fetch,
  new {
    htmlAttributes = new {
      @class = "form-control" , @readonly = "readonly"
    }
  }
)
like image 43
Devesh Kumar Samalia Avatar answered Sep 18 '22 18:09

Devesh Kumar Samalia