1

Possible Duplicate:
View Model IEnumerable<> property is coming back null (not binding) from post method?

I am having a problem that is pretty nearly identical to the problem specified here:

View Model IEnumerable<> property is coming back null (not binding) from post method?

My problem differs from the question linked above, in that in my example I am procedurally creating data with which to populate my view model.

However, none of the proposed solutions fix the issue for me. The method I show below is identical, as far as I can tell, to Travis J's answer, however I also tried the method involving creating an EditorTemplate for my model and/or "sub-model". I'm new to MVC, so I'm most likely doing something staggeringly, obviously wrong.

Here is a streamlined example that shows my problem:

Models:

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

  public Foo(string bar)
  {
    Bar = bar;
  }

  public Foo(){}

}

public class Foos
{
  public List<Foo> ListOfFoos {get;set;}
}

Controller:

public ActionResult Index()
{
    Foos foos = new Foos
    {
      ListOfFoos = new List<Foo>()
    };
    // here I'm procedurally constructing some data to keep my example simple.
    for (int i = 0; i < 10; ++i)
    {
      foos.ListOfFoos.Add(new Foo(i.ToString()));
    }
    return View( foos );
}

[HttpPost]
public ActionResult Index(Foos foos)
{
  ??? foos.ListOfFoos is null here ???
  return View();
}

View:

    @model OCC_Tracker.Models.Foos
    @using (Html.BeginForm())
    {

      <table>
      <tbody>
      @for (int i = 0; i < Model.ListOfFoos.Count; ++i)
      {

        <tr>
          <td>
            @Html.TextBoxFor(m => m.ListOfFoos[i].Bar)
          </td>
         </tr>
      }
      </tbody>
      </table>

    <button>Submit</button>
   }
Community
  • 1
  • 1
D. Reagan
  • 841
  • 1
  • 8
  • 24
  • 1
    If you have exactly the same problem as someone else you don't create a new post with the same question. You can [draw attention to the older question](http://meta.stackexchange.com/questions/7046/how-do-i-get-attention-for-old-unanswered-questions) if it doesn't have good answers. – Servy Nov 15 '12 at 16:58
  • 1
    The other question *does* have (supposedly) good/valid answers. However, these answers, for some reason, do not apply to my situation. According to the link you posted about drawing attention to the older question, none of the proposals really suite my particular situation. – D. Reagan Nov 15 '12 at 17:01
  • If you have the exact same question and the answers don't work, then the question is an exact duplicate, and will be closed accordingly. If something about your situation is different then you need to describe what differentiates your question from the other question. – Servy Nov 15 '12 at 17:04
  • Should I post on the original question with my example? I'm baffled about how, then, to ask for help on Stackoverflow with this particular issue. – D. Reagan Nov 15 '12 at 17:06
  • You can comment on the question or answers as appropriate, you can edit a question/answer to improve it, or you can get 75 (which isn't much) reputation so that you can add a bounty to draw quite a bit of extra attention to the post. Or, as I said, if your question isn't actually an exact duplicate then you'll need to explain *why* it's not; what differentiates it from the other question? – Servy Nov 15 '12 at 17:10
  • Is it considered "appropriate" to comment on an answer with a counter-example, showing a case in which the answer does not work? I'd like to stay within the bounds of what is considered appropriate stackoverflow etiquette, however, my primary goal is to figure out why the hell my example is failing... – D. Reagan Nov 15 '12 at 17:12
  • Commenting on an answer to say that it doesn't work is certainly fine; ideally the answerer will explain what is wrong with your counter example, or they'll fix the problem with their error. The main problem with that is that you need 50 reputation to comment on an answer to a question you didn't ask. – Servy Nov 15 '12 at 17:15
  • I edited my question to show how it differs from http://stackoverflow.com/questions/9915395/view-model-ienumerable-property-is-coming-back-null-not-binding-from-post-me – D. Reagan Nov 15 '12 at 17:15
  • The difference you mention isn't actually a difference. Both posts procedurally create the data to populate the model. – Servy Nov 15 '12 at 17:17
  • That is not true. In the view model that is being discussed, (ProductIndexViewModel), the list of Products is being set from a database: ProductIndexViewModel viewModel = new ProductIndexViewModel { NewProduct = new Product(), *Products = db.Products* }; – D. Reagan Nov 15 '12 at 17:20
  • On another note, I just pasted your code into a new MVC3 project, and it worked fine. On submit, the post method has a populated list of foos. There's nothing wrong with the code you've posted. I suggest you comb through your actual code, comparing it to what you have here, and find the discrepancy. – Forty-Two Nov 15 '12 at 17:23
  • Hmm, I have this exact code running in a project (I copy/pasted straight from my project). I'll start a new project from scratch and see if it works...Thanks for trying it out, I appreciate it. – D. Reagan Nov 15 '12 at 17:25
  • Forty-Two, you are correct. When I create a fresh MVC3 project, and copy my models/controllers/views from my old project into the new project it works fine. Well, this gives me something to go on. Thanks. – D. Reagan Nov 15 '12 at 17:31

0 Answers0