如何从asp.net MVC 4中动态创建的表中检索数据

时间:2013-12-09 08:04:50

标签: asp.net-mvc-4

我是MVC 4的新手,我遇到了问题,我已经动态地使用数据库中的一些数据绑定了一个表,如下面的cshtml页面

<table id="tblFeature">
                            <tr>
                                <th>
                                    Include
                                </th>
                                <th>
                                    Facilities
                                </th>
                                <th>
                                    Description
                                </th>
                            </tr>
                            @foreach (var feature in Model)
                            {
                                <tr>
                                    <td>@Html.EditorFor(item => feature.Checked)
                                    </td>
                                    <td>@Html.DisplayFor(item => feature.Name)
                                    </td>
                                    <td>@Html.TextAreaFor(item => feature.Description, 2, 70, null)
                                    </td>
                                </tr>

                            }
                        </table>

现在我想要更新值,例如,选中或取消选中复选框,更新文本区域中的描述等,以实现我需要在控制器类的列表中添加更新的值然后我将更新数据库中的那些值。但我不知道如何实现这一点,它就像是用简单的英语跟随

foreach(feature in tblFeature)
{
    if(feature is checked)
         {
             featureList.add(feature)
         }
}

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

更改视图以获得正确的绑定:

   @using (Html.BeginForm("Save", "Controller"))
    {   
    <table id="tblFeature">
          <tr>
              <th>
                 Include
               </th>
                <th>
                 Facilities
                  </th>
              <th>
             Description
          </th>
        </tr>
         @for (int i = 0; i < Model.Count(); i++)
         {
           <tr>
            @Html.HiddenFor(m => m[i].ProductSizeID)
           <td>@Html.EditorFor(m => m[i].Checked)
            </td>
             ...
              </td>
             </tr>
        <input type="submit"  value="save"/>
        }

在控制器中发布并保存更改

 [HttpPost]
 public ActionResult Save(IEnumerable<feature> ViewModels)
    {
     foreach (var feature in ViewModels)
     {
        if(feature.Checked)
        {
         featureList.add(feature)
        }
     }
    }