-2

I have one table named announcement when a new data added on this announcement table the count which dispalyed in the _layout.cshtml from a partial view of the same table.. this partial view dispalys the announcements description with count as on the top of the div,. if we add a new announcemetnt on announcement table the count should be automatically incremented without whole page refresh what can i do for this? the code inside the main controller

 [HttpPost]
        public JsonResult Create(Announcet announcement)
        {

            try
            {
                ValidatePageControls(announcement);
                //populate error message if model state is not valid
                if (!ModelState.IsValid)
                {
                    string errorMsg = GetErrorMessage();
                    return Json(new { Result = "ERROR", Message = errorMsg });
                }
                else
                {
                    string entityName = (db as IObjectContextAdapter).ObjectContext.CreateObjectSet<Announcement>().EntitySet.Name;
                    //used to add sequence.
                    Int32 nextVal = new PanERP.Common.Common().getNextSequence(entityName);
                    announcement.AnnouncementId = nextVal;

                    // Set CREATED_BY & CREATED_DATE ...

                    db.Announcement.Add(announcement);
                    //save the entity in DB
                    db.SaveChanges();

                    return Json(new { Result = "OK", Record = announcement });
                }
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

the code inside the partiall view controller

in layout pagere one option is present to increment the count of announcemetn when a new record added in it.. but the count should come after page refresh .. how can i change that

4

1 回答 1

1

就像 Gaz 在评论中所说的那样,如果不检查数据库并进行计数,就无法真正增加记录的数量。如果用户自己添加了一条记录,您可以在客户端增加它,但如果其他人在同一时间(ish)也添加了一条记录,那么您的计数将不同步。

从您的评论来看,如果您对 MVC 和使用 AJAX 很陌生,我衷心建议您查看asp.net/mvc上提供的教程,尤其是 Pluralsight 培训视频。对于这个特定的问题,有一整章专门介绍如何将 AJAX 与 MVC4 结合使用

我知道这不能回答您的问题,但是您不太可能得到答案,因为您的问题非常模糊,并且与此处要提出的问题不符。查看教程,看看你能做什么,然后在遇到更具体的问题时回来。祝你好运!

于 2013-07-09T09:55:00.553 回答