针对数据库中的多个值传递多个Id

时间:2017-12-11 10:53:09

标签: ajax asp.net-mvc-5 sql-insert

我正在处理一项任务,该任务使用包含名称的自动填充文本框项目,文本框或列表框中的商店及其关联的ID(隐藏)并插入到所需的表格中(仅限其相关ID&#39) ; S)。我正在研究mvc 5应用程序。到目前为止,我已经实现了在列表框中添加名称但不是Id的值。并尝试添加列表框值存储在数据库中。以下是我的代码。

注意: - 当按钮点击时,我使用局部视图显示列表框。当前情况是列表框在插入第二个值时会覆盖第一个值。

StudentBatch.cs

  public List<string> selectedids { get; set; }
        public List<string> SelectedNames { get; set; }

Create.cshtml

<div class="form-group">
    <div class="col-md-12">
        @Html.EditorFor(model => model.StudentName, new { id = "StudentName" })
        <input type="button" value="Add Text" id="addtypevalue" />
        <div id="typevaluelist"></div>
    </div>
</div>

<div id="new" style="display:none;">
    <div class="typevalue">
        <input type="text" name="typevalue" />
        <button type="button" class="delete">Delete</button>
    </div>
</div>

<div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Add Students" class="btn btn-default" />
        </div>
    </div>


<script type="text/javascript">

    $(document).ready(function () {
        $("#StudentName").autocomplete({
            //autocomplete: {
            //    delay: 0,
            //    minLength: 1,
            source: function (request, response)
            {
                $.ajax({
                    url: "/Student/CreateStudent",
                    type: "POST",
                    dataType: "json",
                    data: { Prefix: request.term },
                    success: function(data) {
                        try {
                            response($.map(data,
                                function (item)
                                {
                                    return { label: item.FirstName, value: item.FirstName };
                                }));
                        } catch (err) {
                        }
                    }
                });
            },
            messages:
                {
                noResults: "jhh", results: "jhjh"
            }

        });

    });
</script>

   <script>
    $('#addtypevalue').click(function () {
        $(document).ready(function () {


                    var selValue = $('#StudentName').val();

                    $.ajax({
                        type: "GET",
                        url: '@Url.Action("GetListBox", "Student")',
                        dataType: "html",
                        data: { CourseId: selValue },
                        success: function (data) {
                            $("#partialDiv").html(data);
                        },
                failure: function (data) {
                    alert('oops something went wrong');
                }
                });


        });
    });
</script>

GetListBox.cshtml

@model WebApplication1.Models.StudentBatch

@if (ViewBag.Value != null)
{
@Html.ListBoxFor(m => m.SelectedNames, new SelectList(Model.SelectedNames))
}

StudentController.cs

public PartialViewResult GetListBox(string CourseID)
        {
            Context.Student studCont = new Context.Student();
            Models.StudentBatch student = new Models.StudentBatch();
            student.SelectedNames = new List<string>();
            student.SelectedNames.Add(CourseID);
            ViewBag.Value = student;

            return PartialView(student);
        }

0 个答案:

没有答案