MVC JQuery动态表行删除使我的模型为空

本文关键字:我的 模型 删除 JQuery 动态 MVC | 更新日期: 2023-09-27 18:15:00

我有一个奇怪的问题,删除表行。我基本上有一个表,列出了我的模型中的项目,我想做一个批量编辑。我还希望允许删除表行(在提交之前)。一切都很好,除了当我删除表中的第一行时,当我将表单发布到我的控制器时,集合为空。如果我删除任何其他行,集合将完全反映表。只有第一行是空的模型被张贴。

这里是一些javascript

        $("#tableItems").delegate('a.delete', 'click', function (e) {
        e.preventDefault();
        $(this).closest('tr.tableItem).append();
    });

这里是控制器

        [AcceptVerbs(HttpVerbs.Post)]
    [NoCacheAttribute]
    public JsonResult SaveItems(Modelmodel)
    {
        //model.collection is null here if I remove the first row from table
        return Json(new { Result = "OK" });
    }

我有一个编辑器模板,重复这样的行:

<tr class="tableItem">
<td style='width:100%;border-right:solid 1px black;border-top:solid 1px black;background-color:@Model.BackgroundColor;'>@Html.TextBoxFor(x => x.Notes, new { style="width:100%"})</td>
<td style='border-top:solid 1px black;background-color:@Model.BackgroundColor;'><a class="delete" href="#">Delete</a></td>

在我的主视图中,我有一些html看起来像这样:

<table id="tableItems">
    <trbody>
    <tr>
    <td style=' text-align:right;background-color:#F1F1F1;' colspan='6'>
      <input type="submit" value="Submit" />
    </td>
    </tr>
        <tr>
        <td style='background-color:#F1F1F1;' colspan='6'><strong>Name: </strong>@Html.TextBoxFor(x => x.Name)</td>
        </tr>
        <tr>
            <td style='border-right:solid 1px black;border-top:solid 1px black;background-color:#C0C0C0;'><strong>Notes</strong></td>
            <td style='border-top:solid 1px black;background-color:#C0C0C0;'><strong>Actions</strong></td>
         </tr>
        <tr>
            <td colspan="6"></td>
        </tr>
        <tbody>
            @Html.EditorFor(x => x.collection)
        </tbody>
        </trbody>
        </table>

任何帮助将非常感激!

MVC JQuery动态表行删除使我的模型为空

绑定到集合时,razorviewengine使用索引来表示每个记录。模型绑定器处理传入数组的方式是从1开始,直到遇到索引中的空白。

它不能处理间隙。因此,缺少第一个索引意味着它不会尝试绑定其余的索引。

这里是phill hack关于它的一个帖子以及解决方案:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

相关文章: