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?