本文整理汇总了C#中TempDataDictionary类的典型用法代码示例。如果您正苦于以下问题:C# TempDataDictionary类的具体用法?C# TempDataDictionary怎么用?C# TempDataDictionary使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TempDataDictionary类属于命名空间,在下文中一共展示了TempDataDictionary类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RenderPartialToString
public static string RenderPartialToString(this Controller controller, string partialView, ViewDataDictionary viewData, TempDataDictionary tempData)
{
ControllerContext controllerContext = controller.ControllerContext;
var viewEngines = ViewEngines.Engines;
var viewEngineResult = viewEngines.FindPartialView(controllerContext, partialView + ".js");
ViewContext viewContext = new ViewContext(controllerContext, viewEngineResult.View, viewData,
tempData);
var response = viewContext.HttpContext.Response;
response.Flush();
var oldFilter = response.Filter;
Stream filter = null;
try
{
filter = new MemoryStream();
response.Filter = filter;
viewContext.View.Render(viewContext, viewContext.HttpContext.Response.Output);
response.Flush();
filter.Position = 0;
var reader = new StreamReader(filter, response.ContentEncoding);
return reader.ReadToEnd();
}
finally
{
if (filter != null)
{
filter.Dispose();
}
response.Filter = oldFilter;
}
}
示例2: CopyTempDataToModelState
private void CopyTempDataToModelState(ModelStateDictionary modelState, TempDataDictionary tempData)
{
if (!tempData.ContainsKey(TempDataKey)) return;
var fromTempData = tempData[TempDataKey] as ModelStateDictionary;
if (fromTempData == null) return;
foreach (var pair in fromTempData)
{
if (modelState.ContainsKey(pair.Key))
{
modelState[pair.Key].Value = pair.Value.Value;
foreach (var error in pair.Value.Errors)
{
if (!modelState[pair.Key].Errors.Contains(error))
modelState[pair.Key].Errors.Add(error);
}
}
else
{
modelState.Add(pair.Key, pair.Value);
}
}
}
示例3: CopyTempDataToModelState
private void CopyTempDataToModelState(ModelStateDictionary modelState,
TempDataDictionary tempData)
{
if (!tempData.ContainsKey(TempDataKey))
{
return;
}
ModelStateDictionary fromTempData = tempData[TempDataKey]
as ModelStateDictionary;
if (fromTempData == null)
{
return;
}
foreach (KeyValuePair<string, ModelState> pair in fromTempData)
{
if (modelState.ContainsKey(pair.Key))
{
modelState[pair.Key].Value = pair.Value.Value;
foreach (ModelError error in pair.Value.Errors)
{
modelState[pair.Key].Errors.Add(error);
}
}
else
{
modelState.Add(pair.Key, pair.Value);
}
}
}
示例4: PropertiesAreSet
public void PropertiesAreSet()
{
// Arrange
var mockControllerContext = new Mock<ControllerContext>();
mockControllerContext.Setup(o => o.HttpContext.Items).Returns(new Hashtable());
var view = new Mock<IView>().Object;
var viewData = new ViewDataDictionary();
var tempData = new TempDataDictionary();
var writer = new StringWriter();
// Act
ViewContext viewContext = new ViewContext(mockControllerContext.Object, view, viewData, tempData, writer);
// Setting FormContext to null will return the default one later
viewContext.FormContext = null;
// Assert
Assert.Equal(view, viewContext.View);
Assert.Equal(viewData, viewContext.ViewData);
Assert.Equal(tempData, viewContext.TempData);
Assert.Equal(writer, viewContext.Writer);
Assert.False(viewContext.UnobtrusiveJavaScriptEnabled); // Unobtrusive JavaScript should be off by default
Assert.NotNull(viewContext.FormContext); // We get the default FormContext
Assert.Equal("span", viewContext.ValidationSummaryMessageElement); // gen a <span/> by default
Assert.Equal("span", viewContext.ValidationMessageElement); // gen a <span/> by default
}
示例5: ViewContext
public ViewContext(ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData)
: base(controllerContext) {
if (controllerContext == null) {
throw new ArgumentNullException("controllerContext");
}
if (view == null) {
throw new ArgumentNullException("view");
}
if (viewData == null) {
throw new ArgumentNullException("viewData");
}
if (tempData == null) {
throw new ArgumentNullException("tempData");
}
View = view;
ViewData = viewData;
TempData = tempData;
// propagate FormContext, e.g. inside a template
ViewContext originalViewContext = controllerContext as ViewContext;
if (originalViewContext != null) {
FormContext = originalViewContext.FormContext;
}
}
示例6: SetDisplayModeType
public void SetDisplayModeType(ViewDataDictionary viewData, TempDataDictionary tempData, string displayMode, Type displayModeType)
{
var sut = new ViewModelSummary(viewData, tempData, typeof(ViewModelSummary), true, displayMode, displayModeType);
Assert.Equal(displayModeType, sut.DisplayModeType);
Assert.True(sut.HasDisplayMode);
}
示例7: Error
public ActionResult Error( TempDataDictionary data )
{
List<ModelError> errorList = TempData["errors"] as List<ModelError>;
ViewBag.Title = "Something went wrong";
return View( errorList );
}
示例8: AddParameterValuesFromExpressionToTempData
// Copied this method from Microsoft.Web.Mvc.dll (MVC Futures)...
// Microsoft.Web.Mvc.Internal.ExpresisonHelper.AddParameterValuesFromExpressionToDictionary().
// The only change I made is saving the parameter values to TempData instead
// of a RouteValueDictionary.
private IDictionary<string, object> AddParameterValuesFromExpressionToTempData(TempDataDictionary tempData,
MethodCallExpression call)
{
ParameterInfo[] parameters = call.Method.GetParameters();
var parsedParameters = new Dictionary<string, object>();
if(parameters.Length > 0)
{
for(int i = 0; i < parameters.Length; i++)
{
Expression expression = call.Arguments[i];
object obj2 = null;
ConstantExpression expression2 = expression as ConstantExpression;
if(expression2 != null)
{
obj2 = expression2.Value;
}
else
{
Expression<Func<object>> expression3 =
Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof(object)),
new ParameterExpression[0]);
obj2 = expression3.Compile()();
}
tempData[RedirectParameterPrefix + parameters[i].Name] = obj2;
parsedParameters.Add(parameters[i].Name, obj2);
}
}
return parsedParameters;
}
示例9: GetViewHtml
public static string GetViewHtml(string partialViewName, object model)
{
//Fake a context
var context = new ControllerContext();
context.RouteData = HttpContext.Current.Request.RequestContext.RouteData;
context.RequestContext = HttpContext.Current.Request.RequestContext;
ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName);
// Try a regular view if partial not found
if (result.View == null)
{
result = ViewEngines.Engines.FindView(context, partialViewName, "_layout");
}
var viewData = new ViewDataDictionary(model);
var tempData = new TempDataDictionary();
if (result.View != null)
{
var sb = new StringBuilder();
using (var sw = new StringWriter(sb))
{
using (var output = new HtmlTextWriter(sw))
{
var viewContext = new ViewContext(context, result.View, viewData, tempData, output);
result.View.Render(viewContext, output);
}
}
return sb.ToString();
}
return String.Empty;
}
示例10: GetControllerContext
private static ControllerContext GetControllerContext(TempDataDictionary tempData) {
return new ControllerContext() {
Controller = new EmptyController() {
TempData = tempData
}
};
}
示例11: GuardClauses
public void GuardClauses()
{
// Arrange
var controllerContext = new Mock<ControllerContext>().Object;
var view = new Mock<IView>().Object;
var viewData = new ViewDataDictionary();
var tempData = new TempDataDictionary();
var writer = new StringWriter();
// Act & Assert
Assert.ThrowsArgumentNull(
() => new ViewContext(null, view, viewData, tempData, writer),
"controllerContext"
);
Assert.ThrowsArgumentNull(
() => new ViewContext(controllerContext, null, viewData, tempData, writer),
"view"
);
Assert.ThrowsArgumentNull(
() => new ViewContext(controllerContext, view, null, tempData, writer),
"viewData"
);
Assert.ThrowsArgumentNull(
() => new ViewContext(controllerContext, view, viewData, null, writer),
"tempData"
);
Assert.ThrowsArgumentNull(
() => new ViewContext(controllerContext, view, viewData, tempData, null),
"writer"
);
}
示例12: SetModelType
public void SetModelType(ViewDataDictionary viewData, TempDataDictionary tempData, string displayMode, Type displayModeType)
{
var sut = new ViewModelSummary(viewData, tempData, typeof(ViewModelSummary), true, displayMode, displayModeType);
Assert.Equal(true, sut.IsValid);
Assert.Equal(typeof(ViewModelSummary), sut.ModelType);
}
示例13: ToString
/// <summary>
/// Renders a partial view to a string in the specified context.
/// </summary>
/// <param name="partialViewContext">The partial view context.</param>
/// <param name="viewModel">The view model.</param>
/// <param name="cookies">Any cookies that were captures as part of the response.</param>
/// <returns>The view rendered as a string.</returns>
public static string ToString(PartialViewContext partialViewContext, object viewModel, out HttpCookieCollection cookies)
{
string viewName = partialViewContext.ViewName;
using (var writer = new StringWriter())
{
var httpRequest = new HttpRequest("", "http://www.example.com", "");
var httpResponse = new HttpResponse(writer);
// There are still dependencies on HttpContext.Currrent in the ASP.NET (MVC) framework, eg., AntiForgeryRequestToken (as of ASP.NET MVC 4).
var httpContext = new HttpContext(httpRequest, httpResponse) { User = partialViewContext.User };
System.Web.HttpContext.Current = httpContext;
var controllerContext = CreateControllerContext(httpContext);
var viewEngineResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
if (viewEngineResult == null)
{
string message = "The partial view was not found.";
throw new ArgumentException(message, viewName);
}
var view = viewEngineResult.View;
if (view == null)
{
var locations = new StringBuilder();
foreach (string searchedLocation in viewEngineResult.SearchedLocations)
{
locations.AppendLine();
locations.Append(searchedLocation);
}
throw new ArgumentException("The partial view was not found. The following locations were searched: " + locations, viewName);
}
try
{
var viewData = new ViewDataDictionary(viewModel);
var tempData = new TempDataDictionary();
var viewContext = new ViewContextStub(controllerContext, view, viewData, tempData, writer)
{
ClientValidationEnabled = partialViewContext.ClientValidationEnabled,
UnobtrusiveJavaScriptEnabled = partialViewContext.UnobtrusiveJavaScriptEnabled
};
view.Render(viewContext, httpResponse.Output);
cookies = controllerContext.HttpContext.Response.Cookies;
httpResponse.Flush();
}
finally
{
viewEngineResult.ViewEngine.ReleaseView(controllerContext, view);
}
return writer.ToString();
}
}
示例14: Keep_DoesNotThrowException_WhenDataIsNotLoaded
public void Keep_DoesNotThrowException_WhenDataIsNotLoaded()
{
// Arrange
var tempData = new TempDataDictionary(GetHttpContextAccessor(), new SessionStateTempDataProvider());
// Act & Assert
tempData.Keep();
}
示例15: PartialViewHtml
protected string PartialViewHtml(string partialViewName, object model, ViewDataDictionary viewData)
{
var data = new ViewDataDictionary(viewData) {Model = model};
var tempData = new TempDataDictionary();
return RenderPartialToString(this.ControllerContext, partialViewName, data, tempData);
}