Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Model list binding

Here is my model:

public class Items
    {
        public string Foo { get; set; }
        public string Bar { get; set; }
    }

Controller:

public ActionResult Index()
    {
        var model = new List<Items>
                        {
                            new Items
                                {
                                    Foo = "foo",
                                    Bar = "bar"
                                },
                            new Items
                                {
                                    Foo = "ai",
                                    Bar = "ia"
                                },
                            new Items
                                {
                                    Foo = "one",
                                    Bar = "two"
                                }
                        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(List<Items> model)
    {
        return View(model);
    }

View (Index):

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        <div onclick="$(this).remove();">
            @Html.TextBoxFor(model => model[i].Foo) <br/>
            @Html.TextBoxFor(model => model[i].Bar)
        </div>
    }
    <div>
        <input type="submit"/>
    </div>
}

I delete second pair:

    <div onclick="$(this).remove();">
        <input name="[0].Foo" type="text" value="foo"> <br>
        <input name="[0].Bar" type="text" value="bar">
    </div>

    <div onclick="$(this).remove();">
        <input name="[2].Foo" type="text" value="one"> <br>
        <input name="[2].Bar" type="text" value="two">
    </div>

When posting, i get only first pair ("foo" and "bar"). It's because third pair has index "2". I want to get both pairs(Not using FormCollection. I want it to bind automatically). In reality, I have many other inputs on form, so i don't want to reload and reattach indices to each input. Can you help me?

like image 764
karaxuna Avatar asked Sep 06 '12 12:09

karaxuna


People also ask

What is model binding in ASP.NET MVC?

What Is Model Binding? ASP.NET MVC model binding allows mapping HTTP request data with a model. It is the procedure of creating . NET objects using the data sent by the browser in an HTTP request. Model binding is a well-designed bridge between the HTTP request and the C# action methods.

How do you bind a model to view in MVC?

Model binding is a well-designed bridge between the HTTP request and the C# action methods. It makes it easy for developers to work with data on forms (views), because POST and GET is automatically transferred into a data model you specify. ASP.NET MVC uses default binders to complete this behind the scene.

Does MVC use data binding?

asp.net mvc supports data binding. You can bind data to some model and post it back when the form is submitted.


2 Answers

This may be helpful to you....

need to place Hidden field on each item...

MVC3 Non-Sequential Indices and DefaultModelBinder

like image 167
Amit Prajapati Avatar answered Oct 02 '22 11:10

Amit Prajapati


I found solution, thanks to Amit Prajapati:

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        var identifier = Guid.NewGuid();
        <div onclick="$(this).remove();">
            @Html.Hidden("Index", identifier)
            @Html.TextBox("[" + identifier + "].Foo")
            <br/>
            @Html.TextBox("[" + identifier + "].Bar")
        </div>
    }
    <div>
        <input type="submit" />
    </div>
}
like image 43
karaxuna Avatar answered Oct 02 '22 10:10

karaxuna