locked
how to cache data RRS feed

  • Question

  • User-1634604574 posted

    i have tow view i want when i went from view1 to view 2 and then return to view 1 the data will not be removed how can i do it?

    view 1
    
    <input type="text" id="text1">
    <input type="text" id="text2">
    
    <input type="button" id="go to view2">
    
    
    
    view 2
    
    <input type="text" id="text3">
    
    
    <input type="button" id="go to view1">
    

    when i clicked on go to view1 pass value of text3 set into text1 in view 1

    Friday, October 25, 2019 7:30 AM

Answers

  • User-719153870 posted

    Hi zhyanadil,

    It's still the same as the demo. After you added the text3's value in view2, the values is stored in the jquery.session('text3') by $.session.set('text3', $('#text3').val());, you just need to get this session's value to set in wherever you want in view1 with $('#text3').val($.session.get('text3'));.

    Please refer to below demo which is based on 2 controllers:

    Controller1 View1.cshtml:

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>view1</title>
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/jquery.session@1.0.0/jquery.session.min.js"></script>
        <script>
            $(function () {
                $('#text1').val($.session.get('text1'));
                $('#text2').val($.session.get('text2'));
                $('#text3').val($.session.get('text3'));
            })
            function go2()
            {
                $.session.set('text1', $('#text1').val());
                $.session.set('text2', $('#text2').val());
                window.location='@Url.Action("view2", "Controller2") ';
            }
        </script>
    </head>
    <body>
        <div>
            <input type="text" id="text1">
            <input type="text" id="text2">
            <input type="text" id="text3">
    
            <input type="button" id="go to view2" onclick="go2()">
        </div>
    </body>
    </html>

    Controller2 View2.cshtml:

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>view2</title>
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/jquery.session@1.0.0/jquery.session.min.js"></script>
        <script>
            $(function () {
                $('#text3').val($.session.get('text3'));
            })
            function go1()
            {
                $.session.set('text3', $('#text3').val());
                window.location='@Url.Action("view1", "Controller1") ';
            }
        </script>
    </head>
    <body>
        <div>
            <input type="text" id="text3">
            <input type="button" id="go to view1" onclick="go1()">
        </div>
    </body>
    </html>

    Below is the result:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 28, 2019 1:26 AM

All replies

  • User-719153870 posted

    Hi zhyanadil,

    i want when i went from view1 to view 2 and then return to view 1 the data will not be removed

    This can easily done by jquery.session.

    Please refer to below demo:

    view1:

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>view1</title>
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/jquery.session@1.0.0/jquery.session.min.js"></script>
        <script>
            $(function () {
                $('#text1').val($.session.get('text1'));
                $('#text2').val($.session.get('text2'));
            })
            function go2()
            {
                $.session.set('text1', $('#text1').val());
                $.session.set('text2', $('#text2').val());
                window.location='@Url.Action("view2", "Cache") ';
            }
        </script>
    </head>
    <body>
        <div>
            <input type="text" id="text1">
            <input type="text" id="text2">
    
            <input type="button" id="go to view2" onclick="go2()">
        </div>
    </body>
    </html>

    view2:

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>view2</title>
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/jquery.session@1.0.0/jquery.session.min.js"></script>
        <script>
            $(function () {
                $('#text3').val($.session.get('text3'));
            })
            function go1()
            {
                $.session.set('text3', $('#text3').val());
                window.location='@Url.Action("view1", "Cache") ';
            }
        </script>
    </head>
    <body>
        <div>
            <input type="text" id="text3">
            <input type="button" id="go to view1" onclick="go1()">
        </div>
    </body>
    </html>

    Below is the result:

    Best Regard,

    Yang Shen

    Friday, October 25, 2019 9:22 AM
  • User-1634604574 posted

    then  if i want also send value ccc inside view2 to view1 how can i pass it?the same example if view1 in controller1 and view2 in controller2 it mean two different controller

    Friday, October 25, 2019 2:58 PM
  • User-719153870 posted

    Hi zhyanadil,

    It's still the same as the demo. After you added the text3's value in view2, the values is stored in the jquery.session('text3') by $.session.set('text3', $('#text3').val());, you just need to get this session's value to set in wherever you want in view1 with $('#text3').val($.session.get('text3'));.

    Please refer to below demo which is based on 2 controllers:

    Controller1 View1.cshtml:

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>view1</title>
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/jquery.session@1.0.0/jquery.session.min.js"></script>
        <script>
            $(function () {
                $('#text1').val($.session.get('text1'));
                $('#text2').val($.session.get('text2'));
                $('#text3').val($.session.get('text3'));
            })
            function go2()
            {
                $.session.set('text1', $('#text1').val());
                $.session.set('text2', $('#text2').val());
                window.location='@Url.Action("view2", "Controller2") ';
            }
        </script>
    </head>
    <body>
        <div>
            <input type="text" id="text1">
            <input type="text" id="text2">
            <input type="text" id="text3">
    
            <input type="button" id="go to view2" onclick="go2()">
        </div>
    </body>
    </html>

    Controller2 View2.cshtml:

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>view2</title>
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/jquery.session@1.0.0/jquery.session.min.js"></script>
        <script>
            $(function () {
                $('#text3').val($.session.get('text3'));
            })
            function go1()
            {
                $.session.set('text3', $('#text3').val());
                window.location='@Url.Action("view1", "Controller1") ';
            }
        </script>
    </head>
    <body>
        <div>
            <input type="text" id="text3">
            <input type="button" id="go to view1" onclick="go1()">
        </div>
    </body>
    </html>

    Below is the result:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 28, 2019 1:26 AM
  • User-1780421697 posted

    To manage state of controls you can rely on Server Side State OR Client Side State Management.

    On Server Side you can use Server Side Session Or Server Side Cache.

    Session["view1_controlname_value"] = value and you can use

    View1->Textbox-> value='Session[""view1_controlname_value""]' 

    you can populate the values of session on event what you like, for example onblur event of textbox or on exit of page etc

    Similarly you can use Server Cache with unique key name for each user.

    ---------------

    CLIENT SIDE:

    On client Side you can use Session or Local Storage to manage state of control and they exist in a browser session/local storage.

    Tuesday, October 29, 2019 5:05 AM