Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
Ajax BeginForm of list returns only first element of model or null

I'm trying to call an action with an ajax-beginform.

This is my model:

public class MyViewModel
{
    public List<SubViewModel> SubViewModels {get;set;}
}

public class SubViewModel
{
    public string Name {get;set;}
    public int Age {get;set;}
    public bool Active {get;set;}
}

My view looks like this: (I'm iterating over all SubViewModel-Items, and want to get those back later in my DoAction-Method)

@model MyViewModel

@using (Ajax.BeginForm("DoAction", "MyController", null, new AjaxOptions() { HttpMethod = "Post" }, new { @class = "search-form", enctype = "multipart/form-data" }))
{
<table class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Active</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.SubViewModels)
            {
                <tr>
                    <td><input type="text" name="Name" value="@item.Name" /></td>
                    <td><input type="text" name="Age" value="@item.Age" /></td>
                    <td><input type="checkbox" name="Active" value="@item.Active" /></td>
                </tr>
            }
        </tbody>
        </table>
    <input type="submit" value="OK" />
}

And this is my action:

[System.Web.Mvc.HttpPost]
public async Task DoAction([FromBody] MyViewModel model)
{
    // here model is null
}

It always hits the breakpoint in DoAction but:

  • if the type of model is MyViewModel, the property SubViewModels is null.
  • if the type of model is IEnumerable<SubViewModel>, the model is null.
  • if the type of model is SubViewModel, the first dataset is in parameter.

So I guess, the right parameter for this ajax-action is from type SubViewModel.

But I need either MyViewModel or IEnumerable<SubViewModel> to get all listed models items.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to name your inputs such that the modelbinder can bind the post data appropriately. As you have it now, every single item in the list is posted via the same property names: Name, Age, and Active. What you actually need are names like SubViewModels[N].Name, where N is an integer index value. The easiest way to do this to actually use the HTML helpers to generate your inputs, and you'll also need to use a for loop, rather than foreach:

@for (var i = 0; i < Model.SubViewModels.Count(); i++)
{
    <tr>
        <td>@Html.TextBoxFor(m => m.SubViewModels[i].Name)</td>
        <td>@Html.TextBoxFor(m => m.SubViewModels[i].Age)</td>
        <td>@Html.CheckBoxFor(m => m.SubViewModels[i].Name)</td>
    </tr>
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda