查看模型IList<>属性从post方法返回null(不绑定)?

时间:2012-11-15 16:53:32

标签: c# asp.net-mvc asp.net-mvc-3

  

可能重复:
  View Model IEnumerable<> property is coming back null (not binding) from post method?

我遇到的问题几乎与此处指定的问题相同:

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

我的问题与上面链接的问题不同,因为在我的示例中,我在程序上创建用于填充视图模型的数据。

但是,所提出的解决方案都没有为我解决问题。就我所知,我在下面展示的方法与Travis J的答案完全相同,但我也尝试过为我的模型和/或“子模型”创建EditorTemplate的方法。我是MVC的新手,所以我很可能会做出惊人的事情,显然是错的。

这是一个简化的示例,显示了我的问题:

型号:

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;}
}

控制器:

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();
}

查看:

    @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>
   }

0 个答案:

没有答案
相关问题