gpt4 book ai didi

c# - 鉴于 ASP.NET MVC,如何获取多个条目的值(当由 jQuery 动态添加时)

转载 作者:行者123 更新时间:2023-11-30 15:06:09 24 4
gpt4 key购买 nike

这是我的 Controller :

public ActionResult Create() {
Number newNumber = new Number();
return View(newNumber);
}

和 View :

@model PhoneBook.Models.Number
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="../../Scripts/jQuery.AddNewNumber.js" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Contact.Id)
<fieldset>
<legend>Number</legend>
<div class="TargetElements">
<div class="editor-label">
@Html.LabelFor(model => model.PhoneNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PhoneNumber)
@Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NumberKind)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.NumberKind.Title, NumberKinds)
</div>
</div>
<p>
<input class="AddNew" value="Add New" type="button" /></p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

通过按 AddNew 按钮(使用 jQuery AddNewNumber.js)新的输入+DropDown 添加到客户端的表单。这是脚本:

$(document).ready(function () {
$('.AddNew').click(function () {
var targetcloneelements = $(".TargetElements:first").clone();
targetcloneelements.find('input').val('');
targetcloneelements.insertAfter('.TargetElements:last');
});
});

这种方式(Add new)很常见,大家在很多网站都可以看到(所以一定有解决办法),但是retrieval values有问题。当我有一个入口元素(包括 input+DropDown)时,我可以在我的 Controller 的帖子中检索如下值:

[HttpPost]
public ActionResult Create(Number NewNumber, FormCollection collection) {
db.Numbers.Add(NewNumber);
db.SaveChanges();
return RedirectToAction("Index")
}

我读了这篇文章:1 , 2 , 3但是他们都没有任何飞行模式添加条目的解决方案,当我知道实体的数量时它是可能的但在飞行模式下它可以是 1 或 1000

那么,有什么方法可以检索值并将它们添加到数据库中吗?

编辑:

我在 FormCollection 中找到了值,但我不知道如何从集合中提取它们?这是绑定(bind)断点中集合值的图片

Pic

编辑2:

查找以检索所有值:(每个元素都有一个键,如上图所示)

string[] PhoneNumbers = collection.GetValues("PhoneNumber");

最佳答案

您尝试做的是将模型绑定(bind)到对象列表,而您的情况是非顺序的。

如果您遵循如下所述的约定,ASP.NET MVC DefaultModelBinder 可以绑定(bind)任意对象:

Non-Sequential Indices

What happens when you can’t guarantee that the submitted values will maintain a sequential index? For example, suppose you want to allow deleting rows before submitting a list of books via JavaScript.

The good news is that by introducing an extra hidden input, you can allow for arbitrary indices. In the example below, we provide a hidden input with the .Index suffix for each item we need to bind to the list. The name of each of these hidden inputs are the same, so as described earlier, this will give the model binder a nice collection of indices to look for when binding to the list.

您可以从以下位置阅读整篇文章:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

这是代码示例:

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

你的 Controller Action 应该是这样的:

[HttpPost]
public ActionResult Index(IEnumerable<Product> products) {

foreach(var item in products) {

//play with your product here.
}
}

另请参阅以下文章:

Editing a variable length list, ASP.NET MVC 2-style

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

Validating a variable length list, ASP.NET MVC 2-style

http://blog.stevensanderson.com/2010/01/28/validating-a-variable-length-list-aspnet-mvc-2-style/

这两篇文章针对 ASP.NET MVC 2 但在 ASP.NET MVC 3 上同样有效。

关于c# - 鉴于 ASP.NET MVC,如何获取多个条目的值(当由 jQuery 动态添加时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com