2013-09-30 92 views
0

我试图从视图传递一个带有列表属性的模型,以发布后动作。该列表在帖子后返回null,但我们需要列出新项目以更新其信息。我的模型是这样的:MVC4如何将模型指令列表传递给视图

public class Person() 
    { 
    internal string Name {get;set;} 
    internal decimal Age {get;set;} 
    internal List<int> Rate {get;set;} 
    } 

视图填充了他们目前的信息,我们使用foreach通过列表进行迭代,并产生文本框的编辑功能。当人在编辑所需值后提交时,视图将转到[HttpPost]操作,但值为空。

视图被@using(Html.BeginForm(模型))包围,并在提交时,模型被传递给帖子操作,但列表为空。所有其他值都被填充。

任何想法? P.S.我们正在使用c#和Razor View Engine 抱歉,但列表仍然没有传递给模型,这里是4循环。

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

      if (i == 0) 
      { 
        <tr> 
         <th> 
          Vendor Contracts 
         </th> 
         <th> 
         Effective Date 
         </th> 
        </tr> 

      } 
      if (i < 5) 
      { 
       <tr> 
        <td>@Model.VendorContracts[i] 
        </td> 

        <td>@Html.TextBoxFor(model => model.EffectiveDates[i])</td> 
       </tr> 
      } 
      if (i == 5 || i == 10 || i == 15 || i == 20) 
      { 
       @:</table> 
       @:<table> 
       } 
      if (i >= 5) 
      { 
       if (i == 5 || i == 10 || i == 15 || i == 20) 
       { 
       <tr> 
       <th>Vendor Contracts 
       </th> 
       <th> 
       Effective Date 
       </th> 
       </tr> 
       } 
      <tr> 
      <td> 
      @Model.VendorContracts[i] 
      </td> 
      <td>@Html.TextBoxFor(model => model.EffectiveDates[i])</td> 
      </tr> 
      } 
     } 
     </table> 
+0

发表您的看法,以及 – ediblecode

回答

0

我可以给你一个部分答案。要加载列表项,使他们在HttpPost回来,你需要做的这个视图

for(int idx = 0;idx < Model.Rate.Count;idx++) 
{ 
    @Html.TextBox("Foo", Model.Rate[idx]) 
} 

的核心思想是@Html.TextBox("Foo", Model.Rate[idx]),你必须引用率列表中的项目时要使用一个索引。这样,当您回发时,MVC模型活页夹将能够选取列表项目,包括对这些项目的任何更改。

至于拿起用户添加的新项目,我不确定。也许另一个SO用户可以帮忙?但希望我的回答有所帮助。

相关问题