Unable to model bind a collection within a view model

Water Cooler v2

I have a view like so:

@model Foo

@Html.DisplayFor(model => model.FooName);

@{
  int counter = -1;
  }
@foreach(var bar in Model.Bars)
{
  counter++;
  <tr>
    <td><span name = "Bars[@counter].BarName">@bar.BarName</span></td>
  </tr>
}

where:

class Foo
{
  public string FooName { get; set; }

  public ICollection<Bar> Bars { get; set; }
}

class Bar
{
  public string BarName { get; set; }
}

Even though my view has it right, when I post back the Foo, though, it doesn't post back its Bars.

public ActionResult DoStuffWithFoo(Foo foo)
{
  Debug.Assert(foo.Bars != null); // fails
}

I have omitted the unnecessary and obvious parts from this question such as the mark up of a table.

teo van kot

You should understand that on form post you post only values in inputs so you should to something like this:

@for(var i = 0; i < Model.Bars.Count(); i++)
{
  <tr>
    <td><span>@Html.TextBoxFor(x=> Model.Bars[i].BarName)</span></td>
  </tr>
}

Html.TextBoxFor helper create input that you can post to controller.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to bind view model from collection using Knockout

From Dev

Cannot bind model to collection

From Dev

Backbone bind Model to View

From Dev

WPF CustomControl unable to bind dependency property two way to view model

From Dev

Unable to bind Model Property to SelectListItem

From Dev

Unable to bind selectlistitem to model property

From Dev

Bind a collection of a model to only a part of another collection

From Dev

Bind Model to a View asynchronously in Backbone

From Dev

Bind selected item to view model

From Dev

Unable to bind isVisible for input field to model data

From Dev

WPF bind IsEnabled to method on view model

From Dev

WPF MVVM bind Hyperlink RequestNavigate to View model

From Dev

Get Values With One Select and Bind To View Model

From Dev

How to bind View Date to model milliseconds with Angularjs

From Dev

Bind exsiting DOM elements to view model

From Dev

How to bind razor view to model with list of objects?

From Dev

How to bind ListBox to a member of a view model in xaml?

From Dev

Can't bind view model properly

From Dev

bind value in the view to the model before request is sent

From Dev

Bind a custom view to page model in xamarin forms

From Dev

Incorrect model value within foreach in my view

From Dev

Check if a model has data, within the view

From Dev

listen to a collection add/change as a model attribute of a view

From Dev

Calling method from view in model collection?

From Dev

MVC - Sending collection View Model using AutoMapper

From Dev

Model collection inside a Model

From Dev

Model collection inside a Model

From Dev

How to data bind from view to view model in Xamarin?

From Dev

Pass one model from a partial view to a Parent View and bind another model to the same Parent View

Related Related

  1. 1

    Unable to bind view model from collection using Knockout

  2. 2

    Cannot bind model to collection

  3. 3

    Backbone bind Model to View

  4. 4

    WPF CustomControl unable to bind dependency property two way to view model

  5. 5

    Unable to bind Model Property to SelectListItem

  6. 6

    Unable to bind selectlistitem to model property

  7. 7

    Bind a collection of a model to only a part of another collection

  8. 8

    Bind Model to a View asynchronously in Backbone

  9. 9

    Bind selected item to view model

  10. 10

    Unable to bind isVisible for input field to model data

  11. 11

    WPF bind IsEnabled to method on view model

  12. 12

    WPF MVVM bind Hyperlink RequestNavigate to View model

  13. 13

    Get Values With One Select and Bind To View Model

  14. 14

    How to bind View Date to model milliseconds with Angularjs

  15. 15

    Bind exsiting DOM elements to view model

  16. 16

    How to bind razor view to model with list of objects?

  17. 17

    How to bind ListBox to a member of a view model in xaml?

  18. 18

    Can't bind view model properly

  19. 19

    bind value in the view to the model before request is sent

  20. 20

    Bind a custom view to page model in xamarin forms

  21. 21

    Incorrect model value within foreach in my view

  22. 22

    Check if a model has data, within the view

  23. 23

    listen to a collection add/change as a model attribute of a view

  24. 24

    Calling method from view in model collection?

  25. 25

    MVC - Sending collection View Model using AutoMapper

  26. 26

    Model collection inside a Model

  27. 27

    Model collection inside a Model

  28. 28

    How to data bind from view to view model in Xamarin?

  29. 29

    Pass one model from a partial view to a Parent View and bind another model to the same Parent View

HotTag

Archive