Search code examples
asp.netasp.net-corerazor

razor: model that is returned on post does not contain changed data


I have this page:

<form action="@Url.Action("SaveIndex")" method="POST" id="idForm">
  @Html.HiddenFor(m => m.Name[0])       
  @Html.HiddenFor(m => m.AutoCloseWindow)
    <div class="divEditColumns">
        <table cellpadding="0" cellspacing="0" border="0">
            <tr valign="top">
                <td>
                    <div class="Column Column100">
                        <div class="divFieldGroup">
                            <div class="GroupHeadline">Übersetzung</div>
                            <div class="GroupContent">
                                <table cellspacing="" cellpadding="0" border="0">
                                    <tr>
                                        <td class="Label">
                                            <label for="">language</label>
                                        </td>
                                        <td class="Field">
                                            <input type="hidden" name="name1" value="@Model.Name[0]" />
                                            <div class="input-control text">
                                                <input type="text" name="name2" value="@Model.Name[0]" />
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </div>
  
                        </div>
                    </div>
                </td>

            </tr>
        </table>
    </div>
</form>

It displays data from my model and a field on which I can alter the Name value.

Now when I press save this method is fired and my model is returned:

public ActionResult SaveIndex(GlobalTaggingLocalizationModel model, bool autoclosewindow = false)
{
   
    return RedirectToAction("Index", new { autoclosewindow = autoclosewindow, refreshOpener = true, id = 0 });
}

But the model contains the values from the DB (original values that were put onto the model) and my changes to the model are not passed here. I must be forgetting one simple thing

Best,

J


Solution

  • If you want to change the text name2, you need to keep the name attribute is same as the model property.

    try:

    <input type="text" name="Name" value="@Model.Name[0]" />
    

    result:

    enter image description here