本文整理汇总了C#中TempDataDictionary.ContainsKey方法的典型用法代码示例。如果您正苦于以下问题:C# TempDataDictionary.ContainsKey方法的具体用法?C# TempDataDictionary.ContainsKey怎么用?C# TempDataDictionary.ContainsKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TempDataDictionary
的用法示例。
在下文中一共展示了TempDataDictionary.ContainsKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TempData_Save_RemovesKeysThatWereRead
public void TempData_Save_RemovesKeysThatWereRead()
{
// Arrange
var tempData = new TempDataDictionary(GetHttpContextAccessor(), new NullTempDataProvider());
tempData["Foo"] = "Foo";
tempData["Bar"] = "Bar";
// Act
var value = tempData["Foo"];
tempData.Save();
// Assert
Assert.False(tempData.ContainsKey("Foo"));
Assert.True(tempData.ContainsKey("Bar"));
}
示例2: 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);
}
}
}
示例3: 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);
}
}
}
示例4: KeepRetainsAllKeysWhenSavingDictionary
public void KeepRetainsAllKeysWhenSavingDictionary() {
// Arrange
NullTempDataProvider provider = new NullTempDataProvider();
TempDataDictionary tempData = new TempDataDictionary();
Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(c => c.HttpContext.Request).Returns(new Mock<HttpRequestBase>().Object);
tempData["Foo"] = "Foo";
tempData["Bar"] = "Bar";
// Act
tempData.Keep();
tempData.Save(controllerContext.Object, provider);
// Assert
Assert.IsTrue(tempData.ContainsKey("Foo"), "tempData should contain 'Foo'");
Assert.IsTrue(tempData.ContainsKey("Bar"), "tempData should contain 'Bar'");
}
示例5: Notifications
public Notifications(TempDataDictionary tempDataDictionary)
{
if (!tempDataDictionary.ContainsKey(DictionaryName))
{
tempDataDictionary[DictionaryName] = new List<Notification>();
}
_notifications = tempDataDictionary[DictionaryName] as IList<Notification>;
}
示例6: TempData_EnumeratingDictionary_MarksKeysForDeletion
public void TempData_EnumeratingDictionary_MarksKeysForDeletion()
{
// Arrange
var tempData = new TempDataDictionary(GetHttpContextAccessor(), new NullTempDataProvider());
tempData["Foo"] = "Foo";
tempData["Bar"] = "Bar";
// Act
var enumerator = tempData.GetEnumerator();
while (enumerator.MoveNext())
{
var value = enumerator.Current;
}
tempData.Save();
// Assert
Assert.False(tempData.ContainsKey("Foo"));
Assert.False(tempData.ContainsKey("Bar"));
}
示例7: EnumeratingDictionaryMarksValuesForDeletion
public void EnumeratingDictionaryMarksValuesForDeletion() {
// Arrange
NullTempDataProvider provider = new NullTempDataProvider();
TempDataDictionary tempData = new TempDataDictionary();
Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
tempData["Foo"] = "Foo";
tempData["Bar"] = "Bar";
// Act
IEnumerator<KeyValuePair<string, object>> enumerator = tempData.GetEnumerator();
while (enumerator.MoveNext()) {
object value = enumerator.Current;
}
tempData.Save(controllerContext.Object, provider);
// Assert
Assert.IsFalse(tempData.ContainsKey("Foo"), "tempData should not contain 'Foo'");
Assert.IsFalse(tempData.ContainsKey("Bar"), "tempData should not contain 'Bar'");
}
示例8: WrapArrayWithObject
private static IDictionary<string,object> WrapArrayWithObject(dynamic model, TempDataDictionary tempData, ViewDataDictionary viewDataDictionary)
{
var output = new Dictionary<string, object>();
// We want to avoid sending a JSON array as a response.
// See http://haacked.com/archive/2009/06/25/json-hijacking.aspx
// In cases where our model would be converted to an array,
// we wrap it in an object.
object wrappedModel = (typeof(IEnumerable)).IsInstanceOfType(model) ? new { model } : model;
output["Model"] = wrappedModel;
if (tempData.ContainsKey("alert"))
{
output["FlashAlert"] = tempData["alert"].ToString();
}
if (tempData.ContainsKey("confirm"))
{
output["FlashConfirm"] = tempData["confirm"].ToString();
}
if (viewDataDictionary.Any())
{
output["__view__"] = viewDataDictionary
.ToDictionary(viewDataKeyValuePair => viewDataKeyValuePair.Key,
viewDataKeyValuePair => viewDataKeyValuePair.Value);
}
if (viewDataDictionary.ModelState.Any())
{
var errors = viewDataDictionary.ModelState
.Where(p => p.Value.Errors.Any());
if(errors.Any())
output["Errors"] = errors
.ToDictionary(p => p.Key, p => p.Value.Errors.Select(x => x.ErrorMessage).ToArray());
}
return output;
}
示例9: EnumeratingTempDataAsIEnmerableMarksValuesForDeletion
public void EnumeratingTempDataAsIEnmerableMarksValuesForDeletion()
{
// Arrange
NullTempDataProvider provider = new NullTempDataProvider();
TempDataDictionary tempData = new TempDataDictionary();
Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
tempData["Foo"] = "Foo";
tempData["Bar"] = "Bar";
// Act
IEnumerator enumerator = ((IEnumerable)tempData).GetEnumerator();
while (enumerator.MoveNext())
{
object value = enumerator.Current;
}
tempData.Save(controllerContext.Object, provider);
// Assert
Assert.False(tempData.ContainsKey("Foo"));
Assert.False(tempData.ContainsKey("Bar"));
}
示例10: TempData_TryGetValue_MarksKeyForDeletion
public void TempData_TryGetValue_MarksKeyForDeletion()
{
var tempData = new TempDataDictionary(GetHttpContextAccessor(), new NullTempDataProvider());
object value;
tempData["Foo"] = "Foo";
// Act
tempData.TryGetValue("Foo", out value);
tempData.Save();
// Assert
Assert.False(tempData.ContainsKey("Foo"));
}
示例11: TempDataIsADictionary
public void TempDataIsADictionary()
{
// Arrange
TempDataDictionary tempData = new TempDataDictionary();
// Act
tempData["Key1"] = "Value1";
tempData.Add("Key2", "Value2");
((ICollection<KeyValuePair<string, object>>)tempData).Add(new KeyValuePair<string,object>("Key3", "Value3"));
// Assert (IDictionary)
Assert.AreEqual(3, tempData.Count, "tempData should contain 3 items");
Assert.IsTrue(tempData.Remove("Key1"), "The key should be present");
Assert.IsFalse(tempData.Remove("Key4"), "The key should not be present");
Assert.IsTrue(tempData.ContainsValue("Value2"), "The value should be present");
Assert.IsFalse(tempData.ContainsValue("Value1"), "The value should not be present");
Assert.IsNull(tempData["Key6"], "The key should not be present");
IEnumerator tempDataEnumerator = tempData.GetEnumerator();
tempDataEnumerator.Reset();
while (tempDataEnumerator.MoveNext()) {
KeyValuePair<string, object> pair = (KeyValuePair<string, object>)tempDataEnumerator.Current;
Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair), "The key/value pair should be present");
}
// Assert (ICollection)
foreach (string key in tempData.Keys) {
Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(new KeyValuePair<string, object>(key, tempData[key])), "The key/value pair should be present");
}
foreach (string value in tempData.Values) {
Assert.IsTrue(tempData.ContainsValue(value));
}
foreach (string key in ((IDictionary<string, object>)tempData).Keys) {
Assert.IsTrue(tempData.ContainsKey(key), "The key should be present");
}
foreach (string value in ((IDictionary<string, object>)tempData).Values) {
Assert.IsTrue(tempData.ContainsValue(value));
}
KeyValuePair<string, object>[] keyValuePairArray = new KeyValuePair<string, object>[tempData.Count];
((ICollection<KeyValuePair<string, object>>)tempData).CopyTo(keyValuePairArray, 0);
Assert.IsFalse(((ICollection<KeyValuePair<string, object>>)tempData).IsReadOnly, "The dictionary should not be read-only.");
Assert.IsFalse(((ICollection<KeyValuePair<string, object>>)tempData).Remove(new KeyValuePair<string, object>("Key5", "Value5")), "The key/value pair should not be present");
IEnumerator<KeyValuePair<string, object>> keyValuePairEnumerator = ((ICollection<KeyValuePair<string, object>>)tempData).GetEnumerator();
keyValuePairEnumerator.Reset();
while (keyValuePairEnumerator.MoveNext()) {
KeyValuePair<string, object> pair = keyValuePairEnumerator.Current;
Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair), "The key/value pair should be present");
}
// Act
tempData.Clear();
// Assert
Assert.AreEqual(0, tempData.Count, "tempData should not contain any items");
IEnumerator y = ((IEnumerable)tempData).GetEnumerator();
while (y.MoveNext()) {
Assert.Fail("There should not be any elements in tempData");
}
}
示例12: TryGetValueMarksKeyForDeletion
public void TryGetValueMarksKeyForDeletion() {
NullTempDataProvider provider = new NullTempDataProvider();
TempDataDictionary tempData = new TempDataDictionary();
Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
object value;
tempData["Foo"] = "Foo";
// Act
tempData.TryGetValue("Foo", out value);
tempData.Save(controllerContext.Object, provider);
// Assert
Assert.IsFalse(tempData.ContainsKey("Foo"), "tempData should not contain 'Foo'");
}
示例13: SaveRemovesKeysThatWereRead
public void SaveRemovesKeysThatWereRead() {
// Arrange
NullTempDataProvider provider = new NullTempDataProvider();
TempDataDictionary tempData = new TempDataDictionary();
Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
tempData["Foo"] = "Foo";
tempData["Bar"] = "Bar";
// Act
object value = tempData["Foo"];
tempData.Save(controllerContext.Object, provider);
// Assert
Assert.IsFalse(tempData.ContainsKey("Foo"), "tempData should not contain 'Foo'");
Assert.IsTrue(tempData.ContainsKey("Bar"), "tempData should contain 'Bar'");
}
示例14: _AddMessages
private static void _AddMessages(TempDataDictionary tempData, BarData bar, IEnumerable<string> messages)
{
var key = GetTempDataKeyFor(bar);
if (tempData == null) throw new ArgumentNullException("tempData");
if (!tempData.ContainsKey(key))
{
tempData[key] = new List<string>();
}
var statusData = tempData[key] as IList<string>;
messages.ForEach(x => statusData.Add(x));
tempData.Keep(key); // Ensure list will be kept around..
}
示例15: TempData_LoadAndSaveAreCaseInsensitive
public void TempData_LoadAndSaveAreCaseInsensitive()
{
// Arrange
var data = new Dictionary<string, object>();
data["Foo"] = "Foo";
data["Bar"] = "Bar";
var provider = new TestTempDataProvider(data);
var tempData = new TempDataDictionary(GetHttpContextAccessor(), provider);
// Act
tempData.Load();
var value = tempData["FOO"];
tempData.Save();
// Assert
Assert.False(tempData.ContainsKey("foo"));
Assert.True(tempData.ContainsKey("bar"));
}