Chri3
Chri3

Reputation: 133

limiting edit function in asp.net mvc3

I am currently creating an application which references a database using EF in MVC 3. I have used the scaffolding read/write data and want to be able to allow some editing to be done on database entries, but not all fields. Is there a way of blocking a user from editing some fields on the edit form and therefore the database?

Since my initial question I attempted to limit the edit functionality by changing

    <div class="editor-label">
        @Html.LabelFor(model => model.Clause)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Clause)
        @Html.ValidationMessageFor(model => model.Clause)
    </div>

to use the

    <div class="editor-label">
        @Html.LabelFor(model => model.Clause)
    </div>
    <div class="editor-field">
        @Html.DisplayFor(model => model.Clause)
    </div>

However when actually saving the edit, the fields which reference the DisplayFor field come out blank. Is there any better way of doing this so it does not occur?

Many thanks,

Chri3

Upvotes: 1

Views: 94

Answers (1)

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

Well, they are quite a few solutions.

  1. You can show only the fields you wanna edit in your view (not secure, a malicious one could add unwanted fields-values to your form).

  2. You can update manually only the fields you want in your controller.

  3. You can use ViewModels, which will take only the Properties you wanna edit (I would advise you to choose that solution).

EDIT If you want to have the displayed value to be passed in the form, but not "visibly editable", you've got to pass it as an hidden field.

@Html.DisplayFor(model => model.Clause)
@Html.HiddenFor(model => model.Clause)

but that's solution 1. in my answer, probably the worse one.

By the way, if you don't want to edit this field, you should not mind if it comes back blank. Just change the way you update your entities.

Upvotes: 1

Related Questions