Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在ASP.net MVC 4中向数据库添加任意数量的行?_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 如何在ASP.net MVC 4中向数据库添加任意数量的行?

Asp.net mvc 如何在ASP.net MVC 4中向数据库添加任意数量的行?,asp.net-mvc,Asp.net Mvc,我正在尝试向数据库中添加未指定数量的行。这些行是根据用户的请求动态创建的: $(document).ready(function () { $(document).on('click', '#dataTable .add', function () { var row = $(this).closest('tr'); if ($('#dataTable .add').length <= 100) { var clone

我正在尝试向数据库中添加未指定数量的行。这些行是根据用户的请求动态创建的:

$(document).ready(function () {

    $(document).on('click', '#dataTable .add', function () {

        var row = $(this).closest('tr');

        if ($('#dataTable .add').length <= 100) {
            var clone = row.clone();

            // Clear the values.
            var tr = clone.closest('tr');
            tr.find('input[type=text]').val('');
            $(this).closest('tr').after(clone);
        } 
    });

    // Only delete row if there exists more than one.
    $(document).on('click', '#dataTable .removeRow', function () {
        if ($('#dataTable .add').length > 1) {
            $(this).closest('tr').remove();
        }
    });
});
到目前为止,我尝试实现的是一个最大值为100个元素的列表,我更喜欢一个不同的方法,也许没有上限,但这将同时实现,我将我创建的列表传递给我的视图:

// GET: Questions/Create
public ActionResult Create(int questionnaireUID)
{
    List<QUESTION> questions = new List<QUESTION>();
    for (var i = 0; i < 100; i++)
    {
        questions.Add(new QUESTION { QuestionnaireUID = questionnaireUID, Question1 = "" });
    }
    return View(questions);
}
在我看来,这里是我用来填充列表值的相关代码示例,我相信我的问题就在这里…:

<table id="dataTable" name="dataTable">
@if (Model != null && Model.Count() > 0)
{
    <tr>
        @Html.HiddenFor(model => model[i].QuestionnaireUID)
        <td>
            @Html.EditorFor(model => model[i].Question1, new { htmlAttributes = new { @type = "text", @name = "question", @class = "question_input" } })
            @Html.ValidationMessageFor(model => model[i].Question1, "", new { @class = "text-danger" })
            <input type="button" name="addRow[]" class="add" value="Add">
            <input type="button" name="addRow[]" class="removeRow" value="Remove">
        </td>
    </tr>
    i++;
}
</table>
当我尝试使用以下代码将创建的列表保存到数据库中时:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "QuestionnaireUID, Question1")] List<QUESTION> questions)
{
    if (ModelState.IsValid)
    {
        foreach (var question in questions)
        {
            db.QUESTIONS.Add(question);
        }
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(questions);
}
我只能保存列表的第一个元素。无论用户是否动态生成10个列表元素,我的列表的大小始终为1


此外,如果第一个元素有错误,例如它为空,则此错误消息将打印到表中的所有元素。因此,尽管我的列表能够在视图中生成多个元素/行,但对于我的数据库来说,只有一行实际上是有意义/使用的,即第一行。我怎样才能解决这个问题

据我所知,MVC没有提供处理动态/可变长度列表的内置解决方案。有一些自定义解决方案涉及创建帮助程序来处理动态列表对象的发布。这是我过去多次使用的一个,它与旧版本和当前版本的ASP MVC完全兼容。博客使用旧的MVC标记,因此您需要将其更新为MVC3+的RAZOR代码


其中的关键组件是helper类BeginCollectionItem,它以MVC将接受作为操作后参数的方式处理列表的索引。希望这会有所帮助。

您是否查看了向服务器发送HTTP POST请求的有效负载?显示了一些动态添加和删除项目的选项,以及