2

I have model MyObject and then in the view I have

@model IEnumerable<MyProject.Models.MyObject>

and then further down a form :

@using(Html.BeginForm())
{
    @foreach(MyObject m in Model)
    {
        @Html.TextboxFor(x => x.Name)
    }
}

<input type="submit" value="modify" />

I am trying to get this in my controller like this:

[HttpPost]
public void Update(MyObject _myOject)
{
}

When I see the source code, all name is repeated and _myObject is not correctly populated

how do I update a list of objects and save them all at once in MVC?

1 Answer 1

3

You action method is waiting for a single parameter named _myObject. And you want to update list of objects

try something like this:

@using(Html.BeginForm(......))
{
    int i = 0;
    foreach(MyObject m in Model)
    {
        @Html.TextBox(string.Format("_myObjects[{0}].Name", i), m.Name)
        i++;
    }

    <input type="submit" value="modify" />
}

and your action method should look like that:

[HttpPost]
public void Update(IList<MyObject> _myObjects)
{
}

then you will be able to update this list at once

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.