kendo dropdown bindto enum - text string data int - model-view-controller

I am using following Code for my model m:
#(Html.Kendo().DropDownListFor(m => m.*Property*)
.BindTo(Enum.GetValues(typeof(*Enum1*))))
This works fine but of course is not very user friendly. I want to display string values but send int values.
Using .BindTo(Enum.GetNames(typeof (Enum1)).ToList())
does not help as I need to send the actual int values.
Using .BindTo(new List{...}
I also need to call .toString() on my value which needs to be int.
What am I supposed to do?

Convert the enum into a Dictionary and bind the result to the dropdown.
.DataTextField("Value")
.DataValueField("Key")
.BindTo(Enum.GetValues(typeof(*Enum1*)).Cast<*Enum1*>().ToDictionary(e => (int)e, e => e.ToString() );
Hope this helps. Good luck.

Related

Sort the list of strings after string data was added to the list and add method returns void

I saw a few similar questions here and most of the answers shows exactly what I did, however it does not help me in my situation.
That's what I have:
I have a number of dates that I add to the List of strings.
List<string> DateList { get; set; }
DateList = new List<string>();
DateList.AddRange(CashDataList.Select(d => d.RequestedDate.ToLongDateString()).Distinct().ToList());
DateList.AddRange(CheckDataList.Select(d => d.RequestedDate.ToLongDateString()).Distinct().ToList());
DateList.AddRange(CryptDataList.Select(d => d.RequestedDate.ToLongDateString()).Distinct().ToList());
After I added all the dates to my list I select unique dates:
DateList = DateList.Distinct().ToList();
Now, I have the List<string> that contains all my dates which I need to display on my UI in ascending order.
To do that I want to modify my logic to sort my list:
DateList = DateList.Sort();
However, I'm getting the warning that tells me
"Cannot implicitly convert type void to
System.Collection.Generic.List"
I did some research and found out that AddRange method returns void, that's why I have a warning like that.
What is the right way to fix my problem?
You can order the list using LINQ:
DateList = DateList.OrderBy(q => q);
I found the solution to the problem.
I had to change to
List<string> DataList to List<DateTime>
DateList.AddRange(CashDataList.Select(d => d.RequestedDate).Distinct().ToList());
So, my DateList would contain DateTime objects that I can sort. I was not able to sort strings.
And then, in my UI logic, I converted DateTime dates to ToLongDateString format.
That solution worked just fine
Your problem is you didn't look up List<>.Sort. If you had you would discover it sorts the List in-place, and does not have a return value (i.e. returns void).
So, instead of
DateList = DateList.Sort();
you just needed
DateList.Sort();

Codeigniter: how to convert int to string and then insert into database

i have problem with CI when insert data is integer into database.
My field is oauth_uid[varchar(250)], when i use active record insert value is 10205796940433933 into that field, it's become 1.0205796940434E+16
how to fix it!
sorry, my english is not good
This is from php, if you have a int value and you wold like to convert to make a cast to string you only need to make is
$new_v = (string) $int_value;
$data_to_insert = array(
//other values
'oauth_uid' => $new_v
);
I also happened to the same thing and the problem was that I sent GET the form and received it in Controller as POST

Filter records using Linq on an Enum type

I'm hoping this is a simple solution. I have a field (PressType) in a table (Stocks) that is seed populated by using an Enum. The table stores the data as an integer. However when I want to query some data via Linq it gives me issues. I can filter any other fields in the table using this format however on the Enum populated field it says
the "==" operator cannot be applied to operands of type "Models.PressType" and "string".
Any help you could give would be appreciated, thanks.
var test = db.Stocks.Where(x => x.PressType == myValue);
There is nothing wrong with your Linq. Your problem is that myValue is of type string. You need to convert your string to the enum first.
string myValue = SomeControl.Text;
Models.PressType myValueAsEnum = (Models.PressType)
Enum.Parse(typeof(Models.PressType), myValue);
IQueryable<Stock> test = db.Stocks.Where(x => x.PressType == myValueAsEnum);

Populating dropdown list with two columns from database

I'm working on MVC3 project.
At my controller I already have a code that gets the Descr column from StockClass table. After that I fill a ViewBag with this list in order to retrieve it from the view and populate the dropdown list.
Currently is working fine but only shows Descr field (obviously). What i want is populate the dropdown list with two fields (Code and Descr) in this format: "code - descr".
I tried several ways but i cannot find the way to code the #Html helper correctly.
In my controller...
var oc = dba.StockClass.OrderBy(q => q.Code).ToList();
ViewBag.OrderClass = new SelectList(oc, "StockClassId", "Descr");
In my view....
#Html.DropDownList("StockClassID", (SelectList)ViewBag.OrderClass)
Could you please help me?
I don't believe there is an HTML Helper which will do that for you, but you can get what you're after like this:
var oc = dba.StockClass
.OrderBy(q => q.Code)
.ToDictionary(q => q.StockClassId, q => q.Code + " - " + q.Descr);
ViewBag.OrderClass = new SelectList(oc, "Key", "Value");
This also has the advantage of making the uses of StockClassId, Code and Descr refactor-friendly - if you rename those properties you won't be able to compile without updating this bit of code to match.

Linq Query on int using string

I'm trying to query the unique reference number of a table using Linq to Entities. The ID is provided via a textbox and hence a string in my code. Obviously in the database table the field is an integer so I have tried using .ToString first and then .Contains in the same way you would with a varchar(). This doesn't seem to work, with the error coming from the .ToString method.
How do you do this? I have tried converting the textboxes content to an integer but this then means the user would have to enter the exact int instead of a partial number.
Many Thanks
I'm not sure why toString do not work for you. I've tried this to methods. Both returned answers:
List<int> ids = new List<int>() { 111, 211, 311, 422, 522, 622, 733, 833, 933 };
string query = "11";
var result = ids.Where(id => id.ToString().Contains(query));
var result2 = ids.ConvertAll<string>(i => i.ToString()).Where(id => id.Contains(query));
// Both return the first three items
int value;
if(int.TryParse(someTextString,out value))
{
//do work here using integer comparision with 'value'
}
Whatever do you mean by "partial" value? Whatever you are "adding" to the user supplied string, you can still do before you parse the value. Your explanation is not very clear.
If you are indeed trying to make a wildcard search, this will fail using Linq-to-entities against a DB int value. I doubt lte will make much sense of sometable.intvalue.ToString().Contains(). In this case, you might need to do some client side maths to figure out what ranges need retrieving, or to write a stored procedure that does the search at the SQL end..

Resources