views:

57

answers:

1

I have a form that lets the user enter in some text. It will be longer than a few characters so I want to use a TextArea instead of a TextBox.

The Html.TextBoxFor works without issue, and the Html.TextAreaFor works when I create the entry, but does not store the new value when I edit it and shows whatever the value was before I went to edit it upon saving.

On Page:

    <div>
        <label>Work Performed:</label>
        <%: Html.TextAreaFor(model => model.WorkPerformed)%>
        <%: Html.ValidationMessageFor(model => model.WorkPerformed) %>
    </div>

Code Behind for Create:

    maintPerformed.MaintDate = DateTime.Parse(Request.Form["MaintDate"]);
    maintPerformed.WorkPerformed = Request.Form["WorkPerformed"];
    maintPerformedRepository.Add(maintPerformed);
    maintPerformedRepository.Save();
    return RedirectToAction("Details", new { id = maintPerformed.ID });

Code Behind for Edit:

    maintPerformed.MaintDate = DateTime.Parse(Request.Form["MaintDate"]);
    maintPerformed.WorkPerformed = Request.Form["WorkPerformed"];
    maintPerformedRepository.Save();
    return RedirectToAction("Details", new { id = maintPerformed.ID });

What am I missing on the edit side of things?

+2  A: 

In both cases you are redirecting to the Details action. So make sure that maintPerformedRepository.Save() actually does something and most importantly in your Details action look what value is fetched from the data store. I suspect that either the database is not updated or in your Details action you are fetching a wrong value for your model.

Remark: Instead of writing the ugly DateTime.Parse(Request.Form["MaintDate"]); and Request.Form["WorkPerformed"]; pass your view model as argument to your controller action and let the model binder populate it from the request values.

Darin Dimitrov
+1 for recommending the model binder
Alaor