【问题标题】:Submitting a list/Collection with non-sequential indices/positions提交具有非顺序索引/位置的列表/集合
【发布时间】:2019-04-09 08:21:17
【问题描述】:

我有一个包含列表的视图模型,例如:

class School
{
    public List<Student> Students { get; set; }
}
class Student
{
    public int Id { get; set; } 
    public String Name { get; set; } 
}

我有一个表单,我在该表单上提交与单个学校相关的多个学生信息。现在我可以在表单中添加/删除单个学生。

添加工作正常,但我的问题和我的问题与删除学生有关。

所以让我用一个例子来解释一下:

假设我添加了 3 个学生,那么名称和 ID 将以这种方式绑定到模型:

Students[0].Id = "1"

Students[0].Name = "Student A"

Students[1].Id = "2"

Students[1].Name = "Student B"

Students[2].Id = "3"

Students[2].Name = "Student C"

如果我保存它就可以了。但是可以说我删除了学生 Id =“2”。提交时发生的事情是只有 id =“1”的学生 在删除索引(即 id="3" 的学生)未绑定后正在绑定和休息。

我的问题是:删除 id="2" 后是否可以绑定 id="3" ? 或者在适当的情况下,是否可以绑定/提交带有跳过索引的列表。

我发现下面提到的关于 stackoverflow 本身的文章,但我可以从中推断出有点矛盾,或者我可能没有正确理解它们。

Skipping Not possible

Skipping possible

我不擅长解释问题。所以请告诉我是否可以添加任何内容以使其更具描述性。
谢谢你。 示例删除代码: Fiddle for delete Js code

【问题讨论】:

  • 目前作为修复,我在我的学生模型中添加了一个名为 IsDeleted 的布尔属性,单击删除按钮时,我只是将其设为 IsDeleted 并隐藏 Ui 中的内容
  • “绑定”是什么意思,您是说当您删除 ID=2 的学生时,ID=3 也会从列表中删除?您的所有列表索引也是 0,可能是错字?
  • 另外,如果您可以发布添加/删除项目的代码,它会更有用,没有代码很难判断问题出在哪里
  • 绑定是指点击“提交”按钮时发布的值。因此,如果我删除 Id = "2" 的学生,则只有 Student[0].Id 和 Students[0].Name 会发布到服务器,而其余的则不会发布
  • 我已经为添加/删除添加了简单的 html/js 代码。因此,如果我在索引 [3] 处添加 5 个学生并删除 1,那么只会发布索引 [2] 之前的值。希望这更有用。

标签: c# asp.net


【解决方案1】:

我发布此答案以供有同样疑问的其他人将来参考。

如果你有这样的收藏:

public class Book {
    public string Title { get; set; }
    public string Author { get; set; }
    public DateTime DatePublished { get; set; }
}

//Action method on HomeController
public ActionResult UpdateProducts(ICollection<Book> books) {
    return View(books);
}

添加 3 本书后的表单如下所示:

<form method="post" action="/Home/Create">

    <input type="text" name="[0].Title" value="Curious George" />
    <input type="text" name="[0].Author" value="H.A. Rey" />
    <input type="text" name="[0].DatePublished" value="2/23/1973" />

    <input type="text" name="[1].Title" value="Code Complete" />
    <input type="text" name="[1].Author" value="Steve McConnell" />
    <input type="text" name="[1].DatePublished" value="6/9/2004" />

    <input type="text" name="[2].Title" value="The Two Towers" />
    <input type="text" name="[2].Author" value="JRR Tolkien" />
    <input type="text" name="[2].DatePublished" value="6/1/2005" />

    <input type="submit" />
</form>

现在,如果你们想添加删除功能,以便表单可能包含非连续条目,那么可以这样做:

<form method="post" action="/Home/Create">

    <input type="hidden" name="products.Index" value="cold" />
    <input type="text" name="products[cold].Name" value="Beer" />
    <input type="text" name="products[cold].Price" value="7.32" />

    <input type="hidden" name="products.Index" value="123" />
    <input type="text" name="products[123].Name" value="Chips" />
    <input type="text" name="products[123].Price" value="2.23" />

    <input type="hidden" name="products.Index" value="caliente" />
    <input type="text" name="products[caliente].Name" value="Salsa" />
    <input type="text" name="products[caliente].Price" value="1.23" />

    <input type="submit" />
</form>

在上面的示例中,我们为需要绑定到列表的每个项目提供了一个带有 .Index 后缀的隐藏输入。这将为模型绑定器提供一个很好的索引集合,以便在绑定到列表时查找。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 2011-07-08
  • 2017-03-12
相关资源
最近更新 更多