نمایش بلادرنگ اعلامی به تمام کاربران در هنگام درج یک رکورد جدید
نویسنده: سیروان عفیفی
تاریخ: ۱۳۹۳/۰۷/۱۵ ۱۲:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace ShowAlertSignalR.Models
{
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public float Price { get; set; }
public Category Category { get; set; }
}
public enum Category
{
[Display(Name = "دسته بندی اول")]
Cat1,
[Display(Name = "دسته بندی دوم")]
Cat2,
[Display(Name = "دسته بندی سوم")]
Cat3
}
} namespace ShowAlertSignalR.Models
{
public class ProductDbContext : DbContext
{
public ProductDbContext() : base("productSample")
{
Database.Log = sql => Debug.Write(sql);
}
public DbSet<Product> Products { get; set; }
}
} [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
} public ActionResult Index(bool notifyUsers = false)
{
ViewBag.NotifyUsers = notifyUsers;
return View(db.Products.ToList());
} [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index", new { notifyUsers = true });
}
return View(product);
} namespace ShowAlertSignalR.Hubs
{
public class NotificationHub : Hub
{
public void SendNotification()
{
Clients.Others.ShowNotification();
}
}
} @section scripts
{
<script src="~/Scripts/jquery.signalR-2.0.2.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
var notify = $.connection.notificationHub;
notify.client.showNotification = function() {
$('#result').append("<div class='alert alert-info alert-dismissable'>" +
"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>" +
"رکورد جدیدی هم اکنون ثبت گردید، برای مشاهده آن صفحه را بروزرسانی کنید" + "</div>");
};
$.connection.hub.start().done(function() {
@{
if (ViewBag.NotifyUsers)
{
<text>notify.server.sendNotification();</text>
}
}
});
</script>
}