0

I'm creating a new record and returning the new record to the view.

My view:

@using (Ajax.BeginForm((string)ViewBag.EntityControllerUpdateAction, "Contacts",
                new AjaxOptions
                {
                    HttpMethod = "Post",
                    InsertionMode = InsertionMode.Replace,
                    UpdateTargetId = "contactForm",
                    OnComplete = "$('#contacts').find('.notSaving').hide();"
                },
                new { @class = "formEntity clearfix" }))
{
    <div id="contactForm">
        ContactID = @Model.Contact.ID
        @Html.HiddenFor(model => model.Contact.ID, new { @class = "entityID" })
    </div>
}

The reference to @Model.Contact.ID is working fine and displays:

ContactID = XX

However the rendered hidden field looks like this:

<input class="entityID" data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="Contact_ID" name="Contact.ID" value="0" type="hidden">

The value is 0. The fact that the correct ID is displayed demonstrates that my controller is returning the correct value. Why doesn't the view set the value of the hiddenfield correctly?

Thanks.

UPDATE: Here's my controller action:

[HttpPost]
public ActionResult UpdateContact(ContactVM Entity)
{
    try
    {
        if (ModelState.IsValid)
        {

            if (Entity.Contact.ID == 0)      // New entity
                repo.Create<Contact>(Entity.Contact);
            else
            {
                Entity.Contact.State = Entity.Contact.State.ToUpper();
                repo.Update<Contact>(Entity.Contact);
            }
            repo.Save();
        }
    }
    catch (DataException)
    {
        //Log the error (add a variable name after DataException)
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
    }

    return PartialView("~/Views/Contacts/_ContactForm.cshtml", Entity);
}
BKahuna
  • 601
  • 2
  • 11
  • 23
  • You need to show you POST method. I can only guess that your trying to return the same view but are attempting to update the `id` value which is not the correct approach (to explain why `HiddenFor()` displays the initial value, refer [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111)) –  Jan 06 '18 at 11:25
  • Thanks @Stephen - sorry, I didn't think the POST method would make much difference since the model was correctly populated. I've updated my question. It looks like I'm guilty of reusing my view and not updating my ModelState. What is the correct approach? – BKahuna Jan 06 '18 at 20:51
  • Did you not read the answer I linked to? - The value posted back is `0`, then you save the entity which gives it a new `ID` but `0` is still displayed in the input because it uses the value from `ModelState` (which is `0`). You could use `ModelState.Clear()` as per the answer, but you taking the wrong approach. You only return the view is `ModelState` is invalid, otherwise you redirect. –  Jan 06 '18 at 20:55
  • I did but I missed the important line - ModelState.Clear();. I'll give it a try. Thanks. – BKahuna Jan 06 '18 at 21:23
  • As I noted, its the wrong approach. Why are you returning the view after saving the entity (why would the user want to edit it again)? –  Jan 06 '18 at 21:24
  • You've never edited a record, realized you forgot something and wanted to change something. In my UI, the user selects a record and has several "actions" - Edit\Delete\New. Once they select to edit, their "actions" become Save\Discard. After they save they are returned to the original state where the record is displayed and the options are Edit\Delete\New. – BKahuna Jan 06 '18 at 21:45

0 Answers0