Asif Mehmood
Asif Mehmood

Reputation: 984

MVC-5: How to make a Foreign Key Column Disabled only while EditingViews

INTENTION:

Basically, I want to make specific columns non-editable using Data-Annotations In my model class generated through Entity Framework-6.1.3, I have already tried:

//model class
[ReadOnly(true)]
public int DepID { get; set; }

The above column named as DepID is a Foreign-key in this model class.

PROBLEM:

I don't know what's wrong with this. The field value is not being displayed as readonly

Upvotes: 0

Views: 212

Answers (1)

Pedro Benevides
Pedro Benevides

Reputation: 1974

Is it really necessary to show the DepID? I recommend you to pass the DepID to your view as @Html.HiddenFor(m => m.DepID) inside your form. This way, when you send the data back to the server, the property still there.

Upvotes: 1

Related Questions