Exercise Files
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace HPlusSports.Extensions
|
||||
{
|
||||
public class AllowPartialRenderingAttribute : ActionFilterAttribute
|
||||
{
|
||||
public override void OnActionExecuted(ActionExecutedContext filterContext)
|
||||
{
|
||||
var request = filterContext.HttpContext.Request;
|
||||
var result = filterContext.Result as ViewResult;
|
||||
|
||||
if (request == null)
|
||||
return;
|
||||
|
||||
if (request.IsAjaxRequest())
|
||||
{
|
||||
filterContext.Result = new PartialViewResult
|
||||
{
|
||||
TempData = result.TempData,
|
||||
View = result.View,
|
||||
ViewData = result.ViewData,
|
||||
ViewEngineCollection = result.ViewEngineCollection,
|
||||
ViewName = result.ViewName,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
23
Ch07/07_02_Begin/Website/Extensions/HtmlHelperExtensions.cs
Normal file
23
Ch07/07_02_Begin/Website/Extensions/HtmlHelperExtensions.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc.Html;
|
||||
|
||||
namespace HPlusSports
|
||||
{
|
||||
public static class HtmlHelperExtensions
|
||||
{
|
||||
|
||||
public static IHtmlString Rating(this System.Web.Mvc.HtmlHelper html, Models.ProductRating rating)
|
||||
{
|
||||
if (rating == null || rating.ReviewCount == 0)
|
||||
{
|
||||
return new System.Web.Mvc.MvcHtmlString("<span><em>No Rating</em></span>");
|
||||
}
|
||||
|
||||
return html.Partial("_Rating", rating);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
44
Ch07/07_02_Begin/Website/Extensions/TempDataExtensions.cs
Normal file
44
Ch07/07_02_Begin/Website/Extensions/TempDataExtensions.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
|
||||
namespace HPlusSports
|
||||
{
|
||||
public static class TempDataExtensions
|
||||
{
|
||||
const string ErrorMessageKey = "ErrorMessage";
|
||||
const string SuccessMessageKey = "SuccessMessage";
|
||||
|
||||
public static bool HasSuccessMessage(this System.Web.Mvc.TempDataDictionary tempData)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(tempData[SuccessMessageKey] as string);
|
||||
}
|
||||
|
||||
public static string SuccessMessage(this System.Web.Mvc.TempDataDictionary tempData)
|
||||
{
|
||||
return tempData[SuccessMessageKey] as string;
|
||||
}
|
||||
|
||||
public static void SuccessMessage(this System.Web.Mvc.TempDataDictionary tempData, string successMessage)
|
||||
{
|
||||
tempData[SuccessMessageKey] = successMessage;
|
||||
}
|
||||
|
||||
|
||||
public static bool HasErrorMessage(this System.Web.Mvc.TempDataDictionary tempData)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(tempData[ErrorMessageKey] as string);
|
||||
}
|
||||
|
||||
public static string ErrorMessage(this System.Web.Mvc.TempDataDictionary tempData)
|
||||
{
|
||||
return tempData[ErrorMessageKey] as string;
|
||||
}
|
||||
|
||||
public static void ErrorMessage(this System.Web.Mvc.TempDataDictionary tempData, string errorMessage)
|
||||
{
|
||||
tempData[ErrorMessageKey] = errorMessage;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Ch07/07_02_Begin/Website/Extensions/UrlHelperExtensions.cs
Normal file
21
Ch07/07_02_Begin/Website/Extensions/UrlHelperExtensions.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using HPlusSports.Models;
|
||||
|
||||
namespace HPlusSports
|
||||
{
|
||||
public static class UrlHelperExtensions
|
||||
{
|
||||
public static string Product(this System.Web.Mvc.UrlHelper urlHelper, Product product)
|
||||
{
|
||||
return Product(urlHelper, product?.SKU);
|
||||
}
|
||||
|
||||
public static string Product(this System.Web.Mvc.UrlHelper urlHelper, string sku)
|
||||
{
|
||||
return urlHelper.Action("Product", "Products", new { id = sku });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user