简体   繁体   English

客户端上的JS更改后,服务器端更新了模型

[英]updated model on server side after changes in JS on client

I'm new to asp. 我是ASP的新手。 so maybe it is a newbie question. 所以也许这是一个新手问题。

I have 2 lists and submit button inside a model in my client side, After I made some changes in the list I press on the button I want to update my DB with the latest list changes. 我在客户端的模型中有2个列表并提交按钮,在列表中进行了一些更改后,按一下按钮,我想用最新的列表更改来更新数据库。

My problem is that after I made the changes in the client, The lists doesn't updated in the server side. 我的问题是,在客户端中进行了更改之后,列表在服务器端没有更新。

What am I missing here? 我在这里想念什么? How can I update the Server after some client changes? 更改某些客户端后,如何更新服务器?

This is part of my code: 这是我的代码的一部分:

ASPX : (Only the modal) ASPX :(仅模式)

<div class="modal fade" data-backdrop="static" id="ModalSwitchTableSpecie" tabindex="-1" role="dialog" aria-labelledby="ModalSwitchTableSpecieLabel" aria-hidden="true" >
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="H1">Add / Remove Items</h4>
                </div>
                <div class="modal-body row">
                    <select  class="list-group col-lg-5 " id="lstSpecieFullModal" runat="server" style="padding-left:20px;">
                    </select >
                    <div class="col-lg-2">
                        <button  type="button"  class="btn btn-info glyphicon glyphicon-chevron-right" id="btnAddSpecieModal"  onclick="AddSpecie()"></button>
                        <br />
                        <button  type="button" class="btn btn-info glyphicon glyphicon-chevron-left"  id="btnRemoveSpecieModal" onclick="RemoveSpecie()" ></button>
                    </div>
                    <select  class="list-group col-lg-5 " id="lstSpecieCurrentModal" runat="server" style="padding-right:20px;">
                    </select>
                </div>
                <div class="modal-footer">
                    <asp:Button Text="Submit" class="btn btn-success" UseSubmitBehavior="false" data-dismiss="modal" runat="server" id="btnSubmitSpecie" OnClick="btnSubmitchanges_ServerClick"/>
                </div>
            </div>
        </div>
    </div>

JS JS

    <script type="text/javascript" >
    function AddSpecie() {
        var selectedOpts = $('#lstSpecieFullModal option:selected');
        if (selectedOpts.length == 0) {
            alert("Nothing to move.");
            e.preventDefault();
        }
        $('#lstSpecieCurrentModal').append($(selectedOpts).clone());
        $(selectedOpts).remove();
    }(jQuery);
    function RemoveSpecie() {
        var selectedOpts = $('#lstSpecieCurrentModal option:selected');
        if (selectedOpts.length == 0) {
            alert("Nothing to move.");
            e.preventDefault();
        }
        $('#lstSpecieFullModal').append($(selectedOpts).clone());
        $(selectedOpts).remove();
    } (jQuery);
</script>

Code Behind C#: C#背后的代码:

        protected void btnSubmitchanges_ServerClick(Object sender, EventArgs e)
    {
        Button clickedButton = sender as Button;
        dbManager.OpenConnection();
        switch (clickedButton.ID)
        {
            case "btnSubmitSpecie":
                for (int i = 0; i < lstSpecieCurrentModal.Items.Count; i++ )
                    if (!dbManager.CheckIfRecordExist("T_ProductToSpecie", "WHERE RevisionsID = " + SelectedProduct.RevisionsID + " AND SpecieID = " + GetSpecieIDByName( lstSpecieCurrentModal.Items[i].Text)))
                    {
                        dbManager.InsertRemoveFromTable("INSERT INTO T_ProductToSpecie (RevisionsID, SpecieID) VALUES ( " + SelectedProduct.RevisionsID + ", " + GetSpecieIDByName( lstSpecieCurrentModal.Items[i].Text) + ")" );
                    }
                break;
            default:
                break;
        }

    }

In this code as example, I'm adding some new items from the first list to the second and in the server side when I go over the loop, I see the the list size doesn't changed. 在此代码示例中,当我遍历循环时,我将一些新项目从第一个列表添加到第二个列表中,并且在服务器端,列表大小没有改变。

Thanks in advanced for help. 非常感谢您的帮助。

In the asp:Button , you have set UseSubmitBehavior="false" . asp:Button ,您已设置UseSubmitBehavior="false" With this setting, the form data does not get sent to the server side. 使用此设置,表单数据不会发送到服务器端。 You either need to change this back to UseSubmitBehavior="true" or omit this (since true is the default value here). 您需要将其更改回UseSubmitBehavior="true"或忽略它(因为true是此处的默认值)。 The other option is to have some JavaScript code on button click that reads the form data and makes an AJAX call to post the data back to the server side. 另一个选择是在单击按钮时具有一些JavaScript代码,该代码可读取表单数据并进行AJAX调用以将数据发布回服务器端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM