本文整理汇总了C#中ViewDataDictionary.Add方法的典型用法代码示例。如果您正苦于以下问题:C# ViewDataDictionary.Add方法的具体用法?C# ViewDataDictionary.Add怎么用?C# ViewDataDictionary.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ViewDataDictionary
的用法示例。
在下文中一共展示了ViewDataDictionary.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnAuthorization
public override void OnAuthorization( AuthorizationContext filterContext )
{
if ( filterContext == null )
{
throw new ArgumentNullException( "filterContext" );
}
if ( AuthorizeCore( filterContext.HttpContext ) )
{
// ** IMPORTANT **
// Since we're performing authorization at the action level, the authorization code runs
// after the output caching module. In the worst case this could allow an authorized user
// to cause the page to be cached, then an unauthorized user would later be served the
// cached page. We work around this by telling proxies not to cache the sensitive page,
// then we hook our custom authorization code into the caching mechanism so that we have
// the final say on whether a page should be served from the cache.
HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
cachePolicy.SetProxyMaxAge( new TimeSpan( 0 ) );
cachePolicy.AddValidationCallback( CacheValidateHandler, null /* data */);
}
else
{
// auth failed, redirect to not authorized page
ViewDataDictionary viewData = new ViewDataDictionary();
viewData.Add( "PageTitle", "Not Authorised" );
viewData.Add( "Message", "Sorry, you do not have sufficient privileges for this operation." );
filterContext.Result = new ViewResult { ViewName = "NotAuthorised", ViewData = viewData };
//filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary( new { area = "", controller = "Home" } ) );
}
}
示例2: BuildViewData
public void BuildViewData(ViewDataDictionary viewDataDictionary, Order order)
{
viewDataDictionary.Add("Origins", _originRepository.GetAllOrigins().Select(o => new { Name = o, Value = o }));
viewDataDictionary.Add("Customer", order.Customer);
viewDataDictionary.Add("PackageTypes", _packageTypeSelectListBuilder.BuildSelectList());
viewDataDictionary.Add("Destinations", _destinationRepository.GetAllDestinations().Select(d => new { Name = d, Value = d }));
}
示例3: OnException
public override void OnException(ExceptionContext filterContext)
{
if (filterContext == null) throw new ArgumentNullException("filterContext");
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) return;
var exception = filterContext.Exception;
if (new HttpException(null, exception).GetHttpCode() != 500) return;
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
ActionResult result;
if (IsJson)
result = new JsonResult { Data = new { error = filterContext.Exception.Message }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
else {
var dict = new ViewDataDictionary<HandleErrorInfo>(model);
if (filterContext.Controller.ViewData.ContainsKey("KatushaUser")) dict.Add("KatushaUser", filterContext.Controller.ViewData["KatushaUser"]);
if (filterContext.Controller.ViewData.ContainsKey("KatushaProfile")) dict.Add("KatushaProfile", filterContext.Controller.ViewData["KatushaProfile"]);
dict.Add("HasLayout", HasLayout);
result = new ViewResult { ViewName = ExceptionView, ViewData = dict, TempData = filterContext.Controller.TempData };
}
filterContext.Result = result;
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
示例4: WebBlocksLiveEdit
/// <summary>
/// Adds live edit to the web page
/// Use: @Html.WebBlocksLiveEdit(1055, "Wysiwyg")
/// </summary>
/// <param name="html"></param>
/// <param name="blocksRootNode">Root node id for the blocks</param>
/// <param name="stylesheetId">The wysiwyg stylesheet for TinyMCE</param>
/// <returns></returns>
public static string WebBlocksLiveEdit(this HtmlHelper<RenderModel> html, int blocksRootNode, int stylesheetId)
{
ViewDataDictionary viewData = new ViewDataDictionary();
viewData.Add("wbBlocksRootNode", blocksRootNode);
viewData.Add("wbStylesheetId", stylesheetId);
html.RenderPartial("LiveEditRender", null, viewData);
return "";
}
示例5: DropDownListForCategories
public static IHtmlContent DropDownListForCategories(this IHtmlHelper helper, string id, string name, List<Category> list, Guid selected)
{
ViewDataDictionary vdd = new ViewDataDictionary(helper.ViewContext.ViewData);
vdd.Add("Selected", selected);
vdd.Add("Id", id);
vdd.Add("Name", name);
return helper.Partial("HtmlHelper/CategoriesList", list, vdd);
}
示例6: Merge_View_Data_Retains_Destination_Values
public void Merge_View_Data_Retains_Destination_Values()
{
var source = new ViewDataDictionary();
var dest = new ViewDataDictionary();
source.Add("Test1", "Test1");
dest.Add("Test1", "MyValue");
dest.Add("Test2", "Test2");
dest.MergeViewDataFrom(source);
Assert.AreEqual(2, dest.Count);
Assert.AreEqual("MyValue", dest["Test1"]);
Assert.AreEqual("Test2", dest["Test2"]);
}
示例7: OnAuthorization
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (AuthorizeCore(filterContext.HttpContext))
{
SetCachePolicy(filterContext);
}
else if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
// auth failed, redirect to login page
filterContext.Result = new HttpUnauthorizedResult();
}
else if (filterContext.HttpContext.User.IsInRole("SuperUser"))
{
// is authenticated and is in the SuperUser role
SetCachePolicy(filterContext);
}
else
{
ViewDataDictionary viewData = new ViewDataDictionary();
viewData.Add("Message", "You do not have sufficient privileges for this operation.");
filterContext.Result = new ViewResult { MasterName = this.MasterName, ViewName = this.ViewName, ViewData = viewData };
}
}
示例8: OnAuthorization
public void OnAuthorization(AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action","Login"},
{ "controller","Home"}
}
);
//filterContext.Result = new HttpUnauthorizedResult();
}
else
{
ViewDataDictionary viewData = new ViewDataDictionary();
viewData.Add("Message", "You do not have sufficient privileges for this operation.");
filterContext.Result = new ViewResult
{
ViewData = viewData
};
}
//else if (filterContext.HttpContext.User.IsInRole("admin"))
//{
// filterContext.Result = new HttpUnauthorizedResult();
//}
}
示例9: SetControllerView
/// <summary>
/// 设置Controller ViewData
/// </summary>
/// <param name="controller"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public static ViewDataDictionary SetControllerView(this Controller controller,string Key,string Value)
{
//string Name = controller.GetControllerName();
ViewDataDictionary vDict = new ViewDataDictionary();
vDict.Add(Key, Value);
return vDict;
}
示例10: View
protected new virtual ContentResult View(string viewName, object model = null)
{
var controllerContext = this.ControllerContext;
var result = ViewEngines.Engines.FindView(controllerContext, viewName, null);
StringWriter output;
using (output = new StringWriter())
{
var dictionary = new ViewDataDictionary(model);
var dynamic = this.ViewBag as DynamicObject;
if (dynamic != null)
{
var members = dynamic.GetDynamicMemberNames().ToList();
foreach (string member in members)
{
var value = Versioned.CallByName(dynamic, member, CallType.Get);
dictionary.Add(member, value);
}
}
var viewContext = new ViewContext(controllerContext, result.View, dictionary,
controllerContext.Controller.TempData, output);
result.View.Render(viewContext, output);
result.ViewEngine.ReleaseView(controllerContext, result.View);
}
string html = CdnHelper.UseCdn(output.ToString());
html = CustomJavascriptInjector.Inject(viewName, html);
html = MinificationHelper.Minify(html);
return this.Content(html, "text/html");
}
示例11: EvalEvaluatesDictionaryThenModel
public void EvalEvaluatesDictionaryThenModel() {
var obj = new { Foo = "NotBar" };
ViewDataDictionary vdd = new ViewDataDictionary(obj);
vdd.Add("Foo", "Bar");
Assert.AreEqual("Bar", vdd.Eval("Foo"));
}
示例12: RenderPartialWithData
/// <summary>
/// Loads the supplied object to the view data dictionary for use by other partials
/// </summary>
public static void RenderPartialWithData(this HtmlHelper htmlHelper, string partialName, string key, object objectToLoad, ViewDataDictionary viewDataDictionary)
{
if (!viewDataDictionary.ContainsKey(key))
{
viewDataDictionary.Add(key, objectToLoad);
}
htmlHelper.RenderPartial(partialName);
}
示例13: CarregarDadosUsuario
public static void CarregarDadosUsuario(ViewDataDictionary viewData)
{
if (viewData.ContainsKey("logado"))
viewData["logado"] = viewData["logado"];
else
viewData.Add("logado", false);
if (viewData.ContainsKey("tipoUsuario"))
viewData["tipoUsuario"] = viewData["tipoUsuario"];
else
viewData.Add("tipoUsuario", "");
if (viewData.ContainsKey("erroLogin"))
viewData["erroLogin"] = viewData["erroLogin"];
else
viewData.Add("erroLogin", "");
}
示例14: ShouldHandleProxiedObjectsByType
public void ShouldHandleProxiedObjectsByType()
{
var mailMessageStub = MockRepository.GenerateStub<MailMessage>();
var viewData = new ViewDataDictionary();
viewData.Add(mailMessageStub);
var message = viewData.Get<MailMessage>();
Assert.That(message, Is.EqualTo(mailMessageStub));
}
示例15: ShouldGetObjectBasedOnType
public void ShouldGetObjectBasedOnType()
{
var url = new Url("/1");
var identity = new GenericIdentity("name");
var viewData = new ViewDataDictionary();
viewData.Add(identity).Add(url);
viewData.Get(typeof(MailMessage));
}