Pass multiple Id’s against multiple values in database

问题内容:

I’m working on a task that uses autocomplete textbox items containing names, stores in textbox or in listbox with their associated Id’s(hidden) and inserted into the required table (only their related id’s). I’m working on mvc 5 application. So far I’ve achieved to add value in listbox with names but not Id’s. And trying to add the listbox values get stored in database. Below is my code.

Note:- I’m using partial view to display listbox when the button clicks. The current scenario is that it the listbox overwrites the first value when inserting second value.

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);
        }

问题评论:

    
Your autocomplete never gets an ID value because you ever assign one. All you use is the FirstName property.
    
In any case you do it using the .select` event of autocomplete, not using the .click event of your textbox
    
Yes you are right I’m not using ID value, because i dont want to display id as value in autocomplete but want to use it and store it in the database
– user100020
5 hours ago
    
You don’t need to display it – you just need to return it from the controller method and assign it to a property of the autocomplete items, and then get it in the .select event – refer this answer for a typical example
    
And do not clone your input – your generating duplicate id attributes which is invalid html (and it appears you have ignored all the comments I made on your last question)

原文地址:

https://stackoverflow.com/questions/47751324/pass-multiple-ids-against-multiple-values-in-database

添加评论

友情链接:蝴蝶教程