Exclude Property on Update in Entity Framework

后端 未结 5 1133
野性不改
野性不改 2020-11-28 04:53

I\'ve been looking for a proper way to mark a property to NOT be changed when updating a model in MVC.

For example, let\'s take this small model:

cla         


        
5条回答
  •  [愿得一人]
    2020-11-28 05:30

    I guess you don't want the property to be changed just in some cases, because if you are not going to use it never in your application, just remove it from your model.

    In case you want to use it just in some scenarios and avoid its "nullification" in the case above, you can try to:

    • Hide the parameter in the view with HiddenFor:

      @Html.HiddenFor(m => m.Token)

    This will make your original value to be kept unmodified and passed back to the controller.

    • Use TryUpdateModel: http://msdn.microsoft.com/en-us/library/dd460189(v=vs.108).aspx

    Load again your object in the controller from your DBSet and run this method. You can specify both a white list and a blacklist of parameters that shall or shall not be update.

提交回复
热议问题