Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!