Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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 向数据库mvc添加多个内容的不那么懒惰的方法_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 向数据库mvc添加多个内容的不那么懒惰的方法

Asp.net mvc 向数据库mvc添加多个内容的不那么懒惰的方法,asp.net-mvc,Asp.net Mvc,我有一个看法: <div class="form-group"> @Html.LabelFor(model => model.Answers.Answer, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Answers.Answer,

我有一个看法:

<div class="form-group">
    @Html.LabelFor(model => model.Answers.Answer,
    htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Answers.Answer,
         new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Answers.Answer, "",
        new { @class = "text-danger" })
    </div>
</div>
这个应用程序工作得非常好。 但是,我想同时向数据库添加多个答案,而不必每次单击submit

当前的解决方法只是使用split,但这只是懒惰和丑陋


有人有更优雅的解决方案吗

您可以使用列表进行多值插入,但也需要从视图中传递多个答案。视图中有多个文本框以添加多个答案(并根据需要动态添加,然后使用javascript)
public ActionResult Create([Bind(Include = "AId,QId,Answer")] Answers answers)
    {
        if (ModelState.IsValid)
        {
var answerarr = answers.Answer.Split(';');
            foreach (String a in answerarr)
            {
                answers.QId = QIda;
                answers.Answer = a;
                db.Answers.Add(answers);
                db.SaveChanges();
            }
//some code