I want populate a TokenBox from a database using the property tokenBoxSettings.Properties.DataSource
TokenBoxView.cshtml
groupSettings.Items.Add(
formLayoutSettings.Items.Add(i =>
{
i.FieldName = "email";
i.Caption = "Email";
i.NestedExtensionType = FormLayoutNestedExtensionItemType.TokenBox;
TokenBoxSettings tokenBoxSettings = (TokenBoxSettings) i.NestedExtensionSettings;
tokenBoxSettings.Width = 350;
//data binding
tokenBoxSettings.Properties.DataSource = mainController.GetMails();
tokenBoxSettings.Properties.TextField = "email_empresarial";
tokenBoxSettings.Properties.ValueField = "email_empresarial";
tokenBoxSettings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.Contains;
tokenBoxSettings.Properties.ValueSeparator = ';';
})
);
TokenBoxController.cs
//mainController
//I created a dictionary based on the result of select
public Dictionary<string, string> GetMails()
{
var email = db.usuario.ToList().Select(e => new { e.email_empresarial });
var emails = new Dictionary<string, string>();
foreach (var mail in email)
{
correos.Add(mail.ToString(), mail.ToString());
}
return emails;
}
But it shows me the "object explicitly", I only need the value, for example kenneth or manuel
tokenBox list
What am I doing wrong? or with what other approach I can do?
You are specifying same email_empresarial field name for both tokenBoxSettings.Properties.TextField and tokenBoxSettings.Properties.ValueField.
Since you are binding your TokenBox to Dictionary, try changing settings for TextField and ValueField to reference Dictionary Key and Value, like this:
tokenBoxSettings.Properties.TextField = "Value";
tokenBoxSettings.Properties.ValueField = "Key";
Also, in your GetMail() method you have declared the var emails but in the loop you are adding items to the undeclared correos variable. Are you sure you don't have a bug here?
Another note, in the Dictionary returned by GetMails() you populate both dictionary keys and values with the same value of mail.ToString(). Are you sure you really need to use Dictionary to bind your TokenBox? If keys and values are equal you may try going with plain List<string>.
Related
Is it possible to dynamically ignore some properties when saving items with the .NET Object Persistence Model?
I don't want to decorate my class properties with DynamoDBIgnore because sometimes I do want to save the changes being made in those.
I have tried to set IgnoreNullValues to true however this did not work when saving items with a batch.
Code is as follows:
using (var context = new DynamoDBContext(awsClient, _dynamoDbContextConfig))
{
var batch = context.CreateBatchWrite<T>(
new DynamoDBOperationConfig {SkipVersionCheck = true, IgnoreNullValues = true});
batch.AddPutItems(items);
await context.ExecuteBatchWriteAsync(new BatchWrite[] {batch});
}
Shall I use the lower-level API for achieving this?
The lower level API (exposed through IAmazonDynamoDB / AmazonDynamoDbClient) is the only way I have found till now of updating individual properties of an existing DynamoDB document.
An example of this would be :
var updateItemRequest = new UpdateItemRequest
{
TableName = "search_log_table",
Key = new Dictionary<string, AttributeValue>
{
{"PK", new AttributeValue("pk_value")},
{"SK", new AttributeValue("sk_value")}
},
UpdateExpression = $"SET SearchCount = if_not_exists(SearchCount, :start) + :inc",
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{":start", new AttributeValue {N = "0"}},
{":inc", new AttributeValue {N = "1"}}
},
ReturnValues = "UPDATED_NEW"
};
// _client is an instance of IAmazonDynamoDB
return _client.UpdateItemAsync(updateItemRequest);
This inserts a new record with PK = "pk_value", SK = "sk_value" if one does not exist.
The statement UpdateExpression = $"SET SearchCount = if_not_exists(SearchCount, :start) + :inc sets the SearchCount to 1 the first time, and increments the property by 1 on every subsequent invocation
I am able to get a somewhat dynamic Entity Framework search result with the following simplified example, which pulls a single result from the DB or Cache:
string strTableName = "TableName2"
string strColumnName = "MyColumnName"
int intPrimaryKey = 1
Type returnType;
returnType = typeof(TableName1);
string queryResults = null;
switch (strTableName)
{
case "TableName2":
returnType = typeof(TableName2);
break;
}
var refColumnName = returnType.GetProperty(strColumnName );
var query = mydbEntity.Set(returnType).Find(intPrimaryKey );
var queryResults = refColumnName.GetValue(query).ToString();
This can also be adapted for Updating a record:
DataQuery.LeadsEntity.Entry(query).Property(strColumnName ).CurrentValue = "Whatever";
DataQuery.LeadsEntity.SaveChanges();
Is there an equivalent for way for .set(returnType).Add()? I'm not sure if there is a way to do this type of thinking using variable table and column names:
DataQuery.LeadsEntity.Set(returnType).Add(new returnType { PrimayKeyName = 1, refColumnName = "Something" });
If you don't know a priori what's the name of the primary key property, it could be a little painful to get it from the type.
This is the way I found more reliable to retrieve the primary key from the entity type:
private string[] GetKeyNames(DbContext context, Type entityType)
{
ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
//create method CreateObjectSet with the generic parameter of the base-type
MethodInfo method = typeof(ObjectContext).GetMethod("CreateObjectSet", Type.EmptyTypes)
.MakeGenericMethod(entityType);
dynamic objectSet = method.Invoke(objectContext, null);
IEnumerable<dynamic> keyMembers = objectSet.EntitySet.ElementType.KeyMembers;
string[] keyNames = keyMembers.Select(k => (string)k.Name).ToArray();
_keyNamesCache[entityType] = keyNames;
return keyNames;
}
But assuming your primary keys are always a single property, you could use reflection to create the entity object and set its properties like this:
private void CreateEntity(Type entityType, object pkValue, Dictionary<string, object> Columns)
{
// Create the new entity
var entity = Activator.CreateInstance(entityType);
// Get the primary key property name
var pkName = GetKeyNames(context, entityType).First();
// Set Pk value
entityType.GetProperty(pkName).SetValue(entity, pkValue);
// Set other column(s)
foreach (var col in Columns)
{
entityType.GetProperty(col.Key).SetValue(entity, col.Value);
}
// Add the entity to the DbSet
using (var context = new YourContext())
{
context.Set(entityType).Add(entity);
}
}
If I have an entity with a collection property for another entity. What is the best way to add a new entity and it's related entities? The problem I have is that the collection is initially null.
var form = new Form()
{
Name = "TestForm"
};
ctx.Forms.Add(form);
var formField = new FormField()
{
Name = "TestField"
};
form.FormFields.Add(formField);
ctx.SaveChanges();
The form.FormFields property above is null so I get an exception. I know I could set the relationship in the other direction but I haven't defined a Form property on FormFields (and I don't really want to).
So what is the cleanest solution to for this?
The simplest solution is to initialize the collection like this:
var form = new Form() {
Name = "TestForm"
};
ctx.Forms.Add(form);
var formField = new FormField() {
Name = "TestField"
};
if(form.FormFields == null)
form.FormFields = new List<FormField>();
form.FormFields.Add(formField);
ctx.SaveChanges();
Not really sure how to do this but i can cache the dictionary like this:
Cache.Insert("CacheName", Dictionary)
need some direction. the dictionary is two string values taken from a database. The user will input a string and i need to compare it against the values in the cached dictionary.
In general you need to access the object from the cache, cast it, and the use the ContainsKey property. Here is an example:
First add the dictionary to the Cache:
IDictionary<string, string> testDict = new Dictionary<string, string>();
testDict.Add("Test", "test");
Cache.Insert("dict", testDict);
Then, when you need to do so, access the cached object and use it ContainsKey property to determine whether it contains the searched key or not.
var dict = Cache["dict"] as IDictionary<string, string>;
if (dict != null)
{
string testValue = "test";
if(dict.ContainsKey(testValue))
{
/* some logic here */
}
}
You can access the value the following way:
if (dict != null)
{
string testValue = "test";
if(dict.ContainsKey(testValue))
{
/* some logic here */
string value = dict[testValue];
}
}
You can get the dictionary out of the cache by writing
var dict = (Dictionary<X, Y>) cache["CacheName"];
private List<T> GetFieldList()
{
var Fields = new { DisplayName = "MCP", FieldName = "t.MCP", FieldType = 1 };
var FieldList = (new[] { Fields }).ToList();
return FieldList;
}
Should I be able to do something like this?
If I understand correctly your tag "asp.net" this construction will be used as part of data binding.
Just use non generic :
private IList GetFieldList()
{
var Fields = new { DisplayName = "MCP", FieldName = "t.MCP", FieldType = 1 };
IList FieldList = (new[] { Fields }).ToList();
return FieldList;
}
It would be nice handled by all data-bound controls.
I just realized I don't need to use an anonymous list as I know the structure of the data I'm expecting, so I'll just create a small class for it.