4

I have a model with a DateCreated value that is not scaffolded. Coming to GET Edit controller action I see that the model has a correct value that is passed on to the Edit view. Coming back from the view, POST method, the value for DateCreated is DateTime default. It's lost. Does anyone know why? Controller and View are scaffolded.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Product product)
    {
        try
        {
            if (ModelState.IsValid)
            {
                product.DateEdited = DateTime.Now;
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (DataException dex)
        {
            Console.Write(dex.Message);
            ModelState.AddModelError("", reg6.Resources.UnableToSaveChanges);
        }
        return View(product);
    }

@model reg6.Models.Product

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Product</legend>
    <table>
    <tr>
        <td>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
        </td>
        <td>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                <div>
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
            </div>
        </td>
    <tr>
    <tr>
        <td>
            <div class="editor-label">
                @Html.LabelFor(model => model.Description)
            </div>
        </td>
        <td>
            <div class="editor-field">
                @Html.EditorFor(model => model.Description)
                <div>
                    @Html.ValidationMessageFor(model => model.Description)
                </div>
            </div>
        </td>
    <tr>
    <tr>
        <td>
            <div class="editor-label">
                @Html.LabelFor(model => model.BasePrice)
            </div>
        </td>
        <td>
            <div class="editor-field">
                @Html.EditorFor(model => model.BasePrice)
                <div>
                    @Html.ValidationMessageFor(model => model.BasePrice)
                </div>
            </div>
        </td>
    <tr>


    <tr>
    <td>
        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>
    </td>
    <td align="right">
        <input type="submit" value="Create" id='CreateButton' />
    <td>
    </tr>
    </table>

</fieldset>

}

elector
  • 1,327
  • 4
  • 26
  • 43

1 Answers1

6

In your view place a hidden element for the DateCreated

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

All the hidden elements will be posted to the server.

Anto Subash
  • 3,140
  • 2
  • 22
  • 30
  • How can you check, in the html page itself, if the hidden filed value is present? – elector Oct 29 '13 at 08:20
  • The ID filed is hidden as well and I don't see it in the view markup and don't know what to look for in the page itself... – elector Oct 29 '13 at 08:21
  • @elector : The model bind to the element name. Not to the element id. Examine the generated code from the HiddenFor helper and you will understand it. It will generate something like this.``. Even you can include the hidden input field inside the form like this. `` – Haritha Oct 29 '13 at 08:55
  • 1
    This works great. I was struggling with the Date field as it was loosing the value on post. However, can anyone explain why it is lost and we need to add this hidden field to bind it later to the model? Is it true for only DateTime type or we have to include hidden fields for some other types as well? It would be good to know the list. – Sugar Bowl Dec 14 '13 at 00:19