نمایش اخطارها و پیامهای بوت استرپ به کمک TempData در ASP.NET MVC
نویسنده: سیروان عفیفی
تاریخ: ۱۳۹۳/۰۴/۰۵ ۱۲:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<div class="alert">
نمایش اعلانات
</div>
تعدادی کلاس دیگر نیز جهت استفاده از رنگهای مختلف نیز توسط بوت استرپ ارائه شده است:
همچنین اگر مایل بودید میتوانید با افزودن یک دکمه با کلاس close و ویژگی data-dismiss مساوی alert، امکان بستن پیام را در اختیار کاربر قرار دهید:
<div class="alert alert-warning alert-dismissable"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> نمایش اعلان </div>
در ادامه قصد داریم این پیام را بعد از ثبت اطلاعات، به کاربر نمایش دهیم. یعنی در داخل کد، امکان صدا زدن این نوع پیامها را داشته باشیم.
ابتدا کلاسهای زیر را تعریف میکنیم:
کلاس Alert
public class Alert
{
public const string TempDataKey = "TempDataAlerts";
public string AlertStyle { get; set; }
public string Message { get; set; }
public bool Dismissable { get; set; }
} در کلاس فوق خصوصیت یک alert را تعریف کردهایم (از خاصیت TempDataKey جهت پاس دادن alertها به view استفاده میکنیم).
public class AlertStyles
{
public const string Success = "success";
public const string Information = "info";
public const string Warning = "warning";
public const string Danger = "danger";
} public class BaseController : Controller
{
public void Success(string message, bool dismissable = false)
{
AddAlert(AlertStyles.Success, message, dismissable);
}
public void Information(string message, bool dismissable = false)
{
AddAlert(AlertStyles.Information, message, dismissable);
}
public void Warning(string message, bool dismissable = false)
{
AddAlert(AlertStyles.Warning, message, dismissable);
}
public void Danger(string message, bool dismissable = false)
{
AddAlert(AlertStyles.Danger, message, dismissable);
}
private void AddAlert(string alertStyle, string message, bool dismissable)
{
var alerts = TempData.ContainsKey(Alert.TempDataKey)
? (List<Alert>)TempData[Alert.TempDataKey]
: new List<Alert>();
alerts.Add(new Alert
{
AlertStyle = alertStyle,
Message = message,
Dismissable = dismissable
});
TempData[Alert.TempDataKey] = alerts;
}
} public ActionResult Index()
{
var userInfo = new
{
Name = "Sirwan",
LastName = "Afifi",
};
ViewData["User"] = userInfo;
ViewBag.User = userInfo;
TempData["User"] = userInfo;
return RedirectToAction("About");
} @{
ViewBag.Title = "About";
}
<h1>Tempdata</h1><p>@TempData["User"]</p>
<h1>ViewData</h1><p>@ViewData["User"]</p>
<h1>ViewBag</h1><p>@ViewBag.User</p> public class NewsController : BaseController
{
readonly INewsService _newsService;
readonly IUnitOfWork _uow;
public NewsController(INewsService newsService, IUnitOfWork uow)
{
_newsService = newsService;
_uow = uow;
}
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Create(News news)
{
if (ModelState.IsValid)
{
_newsService.AddNews(news);
_uow.SaveChanges();
Success(string.Format("خبر با عنوان <b>{0}</b> با موفقیت ذخیره گردید!", news.Title), true);
return RedirectToAction("Index");
}
Danger("خطا در هنگام ثبت اطلاعات ");
return View(news);
}
[HttpPost]
public ActionResult Delete(int id)
{
_newsService.DeleteNewsById(id);
_uow.SaveChanges();
Danger("اطلاعات مورد نظر با موفقیت حذف گردید!", true);
return RedirectToAction("Index");
}
} @{
var alerts = TempData.ContainsKey(Alert.TempDataKey)
? (List<Alert>)TempData[Alert.TempDataKey]
: new List<Alert>();
if (alerts.Any())
{
<hr />
}
foreach (var alert in alerts)
{
var dismissableClass = alert.Dismissable ? "alert-dismissable" : null;
<div class="alert alert-@alert.AlertStyle @dismissableClass">
@if (alert.Dismissable)
{
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
}
@Html.Raw(alert.Message)
</div>
}
} <div>
@{ Html.RenderPartial("_Alerts"); }
@RenderBody()
</div>
@{ Html.RenderPartial("_Alerts"); } <div id="result"></div>
@using (Ajax.BeginForm(new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "result"
})) [SessionState(SessionStateBehavior.ReadOnly)]