Home > Software engineering >  razor: model that is returned on post does not contain changed data
razor: model that is returned on post does not contain changed data

Time:10-21

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 >
        <table cellpadding="0" cellspacing="0" border="0">
            <tr valign="top">
                <td>
                    <div >
                        <div >
                            <div >Übersetzung</div>
                            <div >
                                <table cellspacing="" cellpadding="0" border="0">
                                    <tr>
                                        <td >
                                            <label for="">language</label>
                                        </td>
                                        <td >
                                            <input type="hidden" name="name1" value="@Model.Name[0]" />
                                            <div >
                                                <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

CodePudding user response:

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

  • Related