1

I have an Edit view which displays some of my fields as follows:

<table>
    <tr>
        <td style="width:40%; vertical-align:top">
            <div class="editor-label">
                @Html.LabelFor(model => model.CREATED_DATE)
            </div>
            <div class="editor-field">
                @Html.DisplayFor(model => model.CREATED_DATE)
                @Html.ValidationMessageFor(model => model.CREATED_DATE)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.LAST_MODIFIED_DATE)
            </div>
            <div class="editor-field">
                @Html.DisplayFor(model => model.LAST_MODIFIED_DATE)
                @Html.ValidationMessageFor(model => model.LAST_MODIFIED_DATE)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.STATUS)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.STATUS, Model.Statuses)
                @Html.ValidationMessageFor(model => model.STATUS)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.SEVERITY)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.SEVERITY, Model.Severities)
                @Html.ValidationMessageFor(model => model.SEVERITY)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.PRIORITY)
            </div>
            <div id="priorityDiv" class="editor-field">
                @Html.EditorFor(model => model.PRIORITY)
                @Html.ValidationMessageFor(model => model.PRIORITY)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.DESCRIPTION)
            </div>
            <div id="descriptionDiv" class="editor-field">
                @Html.EditorFor(model => model.DESCRIPTION)
                @Html.ValidationMessageFor(model => model.DESCRIPTION)
            </div>
        </td>
    </tr>
</table>

In essence I want users to be able to edit any field except for the two at the top, CREATED_DATE and LAST_MODIFIED_DATE. However, when I hit the submit button the two date fields come back as null. My controller code is below.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Alert alert)
{
    if (ModelState.IsValid)
    {
        alert.LAST_MODIFIED_DATE = DateTime.Now;
        db.Entry(alert).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(alert);
}

Is there a way to prevent the user from editing a field in a View without the content of filed being returned to the controller as null?

RikSaunderson
  • 3,505
  • 6
  • 32
  • 50

1 Answers1

4

Add them to your view in hidden fields inside the form:

<div class="editor-field">
    @Html.DisplayFor(model => model.CREATED_DATE)
    @Html.HiddenFor(model => model.CREATED_DATE)
</div>

<div class="editor-field">
    @Html.DisplayFor(model => model.LAST_MODIFIED_DATE)
    @Html.HiddenFor(model => model.LAST_MODIFIED_DATE)
</div>

This will make them post back to your Edit method without the user being able to edit them. You can also remove the ValidationMessages for this fields as they cannot be edited and therefore won't need to display validation messages.

EDIT: As @JLRishe pointed out this would give the users the power to edit the values using their browsers debug tools.

Another solution could be to create a view specific model, and only map the values you want adjusted to your database object. More info about that here: Real example of TryUpdateModel, ASP .NET MVC 3

Community
  • 1
  • 1
Bart Beyers
  • 3,386
  • 1
  • 20
  • 20