Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Session is still present until i dont refresh the page #647

Open
sunnyk11 opened this issue Jan 3, 2019 · 0 comments
Open

Session is still present until i dont refresh the page #647

sunnyk11 opened this issue Jan 3, 2019 · 0 comments

Comments

@sunnyk11
Copy link

sunnyk11 commented Jan 3, 2019

I am using json to save data and fetch data. when i logout from the page, the session should be clear and when i press back button of the browser, and perform any event it should redirect me back to login page, but instead save feature is working when i press back button but when i refresh it redirects me to login page. This should happen without me refreshing the page

Controller:
`public ActionResult Index()
{

            return View();
        
        
    }

    public ActionResult Logout()
    {
        Session.Clear();

        Session.Abandon();
        return RedirectToAction("Index2", "Login");
    }
    public JsonResult GetEvents(string username)
    {
       
        //var status = false;
        using (MeetingEntities db = new MeetingEntities())
            {
                var events = db.MeetingDetails.Where(a => a.User_name == username).ToList();


                return new JsonResult { Data = events, JsonRequestBehavior = JsonRequestBehavior.AllowGet };


            }
        

    }
   

    [HttpPost]
    
    public JsonResult SaveEvent(MeetingDetail Ms)
    {
        
           var status = false;
            using (MeetingEntities db = new MeetingEntities())
            {

                if (Ms.MeetingId > 0)
                {
                    var v = db.MeetingDetails.Where(a => a.MeetingId == Ms.MeetingId).FirstOrDefault();
                    if (v != null)
                    {
                        v.User_name = Ms.User_name;
                        v.Subject = Ms.Subject;
                        v.Start = Ms.Start;
                        v.End = Ms.End;
                        v.Description = Ms.Description;
                        v.IsfullDay = Ms.IsfullDay;
                        v.ThemeColor = Ms.ThemeColor;
                    }
                }
                else
                {
                    db.MeetingDetails.Add(Ms);
                }
                db.SaveChanges();
                status = true;
            }
            return new JsonResult { Data = new { status = status } };
        

        
        //var username = Session["UserID"].ToString();
       
    }

    public JsonResult deleteEvent(int eventID)
    {
        var status = false;
        {
            using (MeetingEntities db=new MeetingEntities())
            {
                var v = db.MeetingDetails.Where(a => a.MeetingId == eventID).FirstOrDefault();
                if (v != null)
                {
                    db.MeetingDetails.Remove(v);
                    db.SaveChanges();
                    status = true;
                }
            }
        }
        return new JsonResult { Data = new { status = status } };
    }

    
}`

View:
$(document).ready(function () {

        var username = '@Session["UserID"].ToString()';
        //var username = $('#session').val();
        var events = [];
        var selectedEvent = null;
        fetchEvent();
        //alert(session).val();
        function fetchEvent() {
            events = [];
            $.ajax({
                type: "GET",
                url: '/home/GetEvents',
                data: { 'username': username },
                success: function (data) {
                    $.each(data, function (i, v) {
                        events.push({

                            username: v.User_name,
                            eventID: v.MeetingId,
                            title: v.Subject,
                            description: v.Description,
                            start: moment(v.Start),
                            end: v.End != null ? moment(v.End) : null,
                            color: v.ThemeColor,
                            allDay: v.IsfullDay
                        });
                    });
                    GenerateCalendar(events);
                },
                error: function (error) {
                    alert('failed');
                }

            });
        }

$('#btnSave').click(function () {
alert(username);
if ($('#txtsubject').val().trim() == "") {
alert('Subject required');
return;
}
if ($('#txtstart').val().trim() == "") {
alert('Start Date required');
return;
}
if ($('#txtsubject').val().trim() == "") {
alert('Subject required');
return;
}
if ($('#chkisfullday').is(':checked') == false && $('#txtend').val().trim() == "") {
alert('End Date required');
return;
}
else {
var startDate = moment($('#txtstart').val(), "DD/MM/YYYY HH:mm A").toDate();
var endDate = moment($('#txtend').val(), "DD/MM/YYYY HH:mm A").toDate();
if (startDate > endDate) {
alert('Invalid end date');
return;
}
}

            var data = {
                MeetingId: $('#hdEventID').val(),
                Subject: $('#txtsubject').val().trim(),
                Start: $('#txtstart').val().trim(),
                End: $('#chkisfullday').is(':checked') ? null : $('#txtend').val().trim(),
                Description: $('#txtDescription').val(),
                ThemeColor: $('#ddThemecolor').val(),
                IsfullDay: $('#chkIsFullDay').is(':checked'),
                User_name: username
            }

            SaveEvent(data);
            //alert(username);
            //call function for submit data on the server
        });
        function SaveEvent(data) {

            $.ajax({
                type: "POST",
                url: '/home/SaveEvent',
                data: data,
                success: function (data) {
                    if (data.status) {
                        //Refresh the calendar
                        fetchEvent();
                        $('#myModalSave').modal('hide');
                        //alert(username);
                    }
                },
                error: function () {
                    alert('failed');
                }
            });
        }

    });
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant