Kendo UI: Get MultiSelect selected values as a comma separated string - c#

I'd appreciate if someone could help with my issue.
I have an entity with field PAYMENT_CURRENCIES of string type, that should store comma separated values, i.e. "USD,EUR,AED" (or any other separation char).
In my View:
#Html.Kendo().MultiSelectFor(model => model.Contract.PAYMENT_CURRENCIES).BindTo(context.Currencies).DataTextField("CODE").DataValueField("CODE").Placeholder("Add currency...")
The problem is when I submit the form i receive only first selected value in the Controller.
I would not like to change the datatype of the field for IEnumerable.
Is there a way to receive all selected values as a string with some separator?
Thanks a lot

I don't think that you can automatically convert your multi select input value(s) to a single string.
So what you can do is:
Use a viewModel (ContractViewModel) which contains a List
Or use javascript to "convert" your input value(s) to a single string separated with any separator you want

Add an array-property to your model:
public string[] PAYMENT_CURRENCIES_LIST
{
get
{
return PAYMENT_CURRENCIES?.Split(',');
}
set
{
PAYMENT_CURRENCIES = string.Join(",", value);
}
}
Then use this property in your view:
#Html.Kendo().MultiSelectFor(model => model.Contract.PAYMENT_CURRENCIES)...
So the array-property maps to the Kendo-Multiselect and translates the values to/from the original field.

I had the same requirement as yours, & couldn't find a decent solution, That's how I solved it:
Create a new Property in your ViewModel public List<string> SelectedCurrencies { get; set; }
Configure your MultiSelect Kendo helper to bind to the newly created property #Html.Kendo().MultiSelectFor(model => model.SelectedCurrencies)
.BindTo(context.Currencies)
.DataTextField("CODE")
.DataValueField("CODE")
.Placeholder("Add currency...")
To Save: Now when you hit your action method, just set your comma separated field PAYMENT_CURRENCIES = string.Join(",", viewModel.SelectedCurrrencies);
To Read: SelectedCurrencies = PAYMENT_CURRENCIES.Split(',').ToList();

Related

Getting an IEnumerable of all values in a certain column

I'm currently working with the Atata framework and using a Kendo grid. What I'm trying to achieve is getting an IEnumerable or a collection containing all the values in a column. The kendo grid is defined as below:
public KendoGrid<TransactionGroupsRow, _> TransactionGroupsGrid { get; private set; }
public class TransactionGroupsRow : KendoGridRow<_>
{
[FindByColumnHeader("Group Name")]
public Link<_> GroupName { get; private set; }
}
I not only want a collection of all the links GroupName, I specifically want the text component of them i.e. what the link would read like on the screen.
So in this example I would want an IEnumerable containing "002999" and "003999".
Let me know if I need to provide additional detail. Thanks in advance!
There are 2 ways to do that.
Use SelectData method. It works quite fast for list of items with count <= 50.
// Just get all items:
IEnumerable<string> groupNames = page.TransactionGroupsGrid.Rows
.SelectData(x => x.GroupName.Content.Value).Value;
// Assert items:
page.TransactionGroupsGrid.Rows
.SelectData(x => x.GroupName.Content.Value).Should.Contain("002999", "003999");
The faster way is using SelectContentsByExtraXPath method. This one will work very fast even for a table with 1000 rows. But you need to specify a relative XPath to the table cell td element inside a row tr element. This can also be extracted to the page object class as a method.
// Just get all items:
IEnumerable<string> groupNames = page.TransactionGroupsGrid.Rows
.SelectContentsByExtraXPath("td[1]", "Group Names").Value;
// Assert items:
page.TransactionGroupsGrid.Rows
.SelectContentsByExtraXPath("td[1]", "Group Names").Should.Contain("002999", "003999");
You can also check Performance Practices for ControlList Atata sample project.

Binding Field from List of object to a Dropdownlist MVC

I have a list of a certain object, each object contains fields. I need to do a DropDownListFor or DropDownList for that field within that object but its not binding
I initially tried this with a List and this works fine for binding to a normal object field. I then tried converting to SelectList() by doing
#Html.DropDownListFor(model => Model.MyListOfObjects[i].FieldName, new SelectList(Model.MySelectList, "Value", "Text"))
I then tried to set the "Selected" property on the SelectListItem I needed to be selected but still no luck
for (var i = 0; i < Model.MyListOfObjects.Count; i++){
#Html.DropDownListFor(model => Model.MyListOfObjects[i].FieldName, Model.MySelectList)
}
Model.MySelectList is List<SelectListItem>
public List<MyCustomObject> MyListOfObjects {get;set;} //populated
public class MyCustomObject{
public string FieldName {get;set;}
}
So in the end, I need the value of Model.MyListOfObjects[i].FieldName tp be selected from the Model.MySelectList(). The value in the select list does match so I cant see whats wrong
Thank you. Appreciate any help
EDIT: I know I can use "EditorFor" templates for the object used in Model.MyListOfObjects but I would prefer to avoid it if possible as I am using that object with other things so if I need another version of the object in editorfor then I wouldnt be able to do it. As a last resort though, I will just have to change the object to a unique one and go down the editorfor route
selectListItem contains text and value.
You need to bind value field to dropdown not text field.
i.e. model => Model.MyListOfObjects[i].FieldValue
Check This will help you

Dynamically Set Model Properties in ASP.NET MVC

I have a long list of check boxes that I need to display on a form. I get the list of check boxes from a table in the database and render them on the page. When the page is saved, I am saving the field name and value (as they are stored in the database) of each selected item.
I also have a model class that all of these check box fields map to, but I was thinking, since I have the field name and value, I would be able to set only the properties for which I have a field/value.
I was thinking I could just do something like this, but I know the syntax isn't correct:
private void CreatePPAIndication(ReferralFormViewModel viewModel, Guid personId, Guid encId)
{
var ppaIndication = new PPAIndication();
ppaIndication.enterprise_id = "00001";
ppaIndication.practice_id = "0001";
ppaIndication.person_id = personId;
ppaIndication.enc_id = encId;
// set more properties...
// Here is where I would set a property for each selected check box.
foreach (var indication in viewModel.Indications.Where(o => o.IsSelected))
{
var result = "ppaIndication." + indication.FieldName + "(" + indication.FieldValue + ")";
}
// How can I essentially "execute" the string in the line above?
_context.PPAIndications.Add(ppaIndication);
}
So, basically, I am wondering how I can essentially "execute" the "result" string as real code. Thank you in advance for any advice!
you can use reflection for that
something like
GetType().GetProperty(o.FieldName).GetValue(indication, null);
to get the value
or
GetType().GetProperty(o.FieldName).SetValue(indication, o.Value);
to set the value

filter datagridview using another datagridview

I have a text file with size about 10-20 MB.
I want to perfom this options.
1) read the text file and split.
2) move all the data in each line into a datagrid view.
3) create an addition datagridview. that the user can define which line can be visible and which not per a values of 1 column (filter).
I have written a code however however it take a long time.
reading to the first datagrid view the text takes about 40 secound.
when I tried to filter it takes about also 40-50 secound till the user filter it,
is there a way to reduce the time? what is the best way to do this kind of thing? and does using a datasource can be helpful?
Thanks,
I think its better to use linq.
Read your file like this and you have a list of file lines.
var lst = System.IO.File.ReadAllLines(FilePath).ToList();
Now you can filter your list by linq statements.
And you can simply show the list in your datagrid.
If you want to show all data in datagrid, it takes time.
You can also create a class for that purpose
public class MyLine
{
public string Line {get; set; }
public bool IsVisible { get; set; }
}
Then you can read the file and have a list or your class, like this:
var lst = System.IO.File.ReadAllLines("")
.Select(x => new MyLine() { Line = x, IsVisible = true });
Then for getting visible ones write this query:
var Visibles = lst.Where(x => x.IsVisible);
Hope it helps.

How to display both Name and ID of a program in Dropdownlist control

My project display's program names from the database into dropdownlist and each program has a ID. I want to display both name and id in the dropdownlist so that they can be differenciated from each other.
eg: 'california lifeline (CLA)'
where 'california lifeline' is the name of the program and the id is 'CLA'. I have created a stored procedure which displays data based on the program ID.
This the code of my dropdownlist control.
private void LoadProgramName()
{
_drp_program = (DropDownList)Page.FindControl("bodyuc$drp_program");
dsprg = rProxy.GlobalFetchFromDB(strCountyName, "DBO.oea_sp_get_onoff_programNames");
_drp_program.DataSource = dsprg;
_drp_program.DataTextField = "PROG_NAME";
_drp_program.DataValueField = "PROGRAM_ID";
_drp_program.DataBind(); ;
ListItem lst_prog = new ListItem();
lst_prog.Value = "";
lst_prog.Text = "--Select One--";
_drp_program.Items.Insert(0, lst_prog);
_drp_program.Items.Insert(1, "ALL");
}
BTW, the dropdown is a part of pagecontrol.
Help is Appriceated.
If your GlobalFetchFromDB() returns instances of an object which you can "extend" as a partial class, then I'd add a "helper" property to the class that formats your displayed string.
One advantage to this is that the format can be dependent on the values of other properties in the instance.
For example:
public partial class ProgramItem
{
public string DisplayName { get { return PROG_NAME.ToUpper(); } }
}
Then use _drp_program.DataTextField = "DisplayName";
An alternative is to construct a class that encapsulates the returned items and adds the DisplayName as a decorator.
Instead of binding _drp_program to dsprg, loop through the results in dsprg and add each item individually.
foreach(dsprgObject in dsprg)
{
_drp_program.Items.add(new ListItem(dsprgObject .ColA + " " + dsprgObject .ColB, dsprgObject.PROGRAM_ID));
}
You can try
_drp_program.DataSource = from item in dsprg
select new
{
PROG_NAME = string.Format("{0}({1})", item.PROG_NAME, item.PROGRAM_ID) ,
PROGRAM_ID = item.PROGRAM_ID
};
instead of
_drp_program.DataSource = dsprg;
Usually the fastest way is to concatenate ID and NAME in an SQL query (but you're using an sp so that's a bit difficult). Also, since you binding your control in code, you can concatenate ID and Name in code and populate your dropdownlist manually (either by calling Items.Insert or by binding your dropdown to a collection which members have a field/property that contains the concatenated value). AFAIK, you can only specify a field/property name for DataTextField and DataValueField, no expressions etc...

Categories

Resources