Search code examples
listasp.net-mvc-5http-postviewmodelhtml.hiddenfor

Keep ViewModel list populated from HTTPGet to HTTPPost ActionResult


In an MVC5 internet application, how can I keep the contents of a ViewModel List, between the HTTPGet request and the HTTPPost request.

I have done some searching, but am not sure exactly what term to search for.

Here is my situation:

I have a ViewModel, that has the following list populated in the HTTPGet ActionResult:

public List<string> azureBlobFullFileNames { get; set; }

In the HTTPPost ActionResult, the list is null.

How can I still have the list populated with values in the HTTPPost ActionResult?

I have added the following code after the @Html.AntiForgeryToken() View line of code:

@Html.HiddenFor(model => model.azureBlobFullFileNames)

However, the list is still null in the HTTPPost ActionResult.

Can I please have some help with this code?

Thanks in advance


Solution

  • Generate a control for element in the collection using a for loop.

    for(int i = 0; i < Model.azureBlobFullFileNames.Count; i++)
    {
      @Html.HiddenFor(m => m.azureBlobFullFileNames[i])
    }