Select1 only works for integer properties


With e.g. form.Select1(c => c.thisCanOnlyBeInt, Model.availableListItems [, ...]) there only an integer can be submitted. It is not possible to use a Guid as the primary Key and use form.Select1(c => c.ParentGuidAsString, Model.existingParents.ToList(), t => t.Guid.ToString("N"), t => t.Label); whereas i want the user to select a parent item: the user only sees the label of the item and behind that an Guid will be submitted as string. I'd like to submit a string using the Select1 feature.
Closed May 5, 2009 at 9:20 AM by JontyMC


JontyMC wrote Apr 1, 2009 at 10:08 AM

It is possible to use any datatype with Select1. Internally ToString() gets called on the object.

However, there is an issue with what you are trying to do. The Select1 works on an object basis. You can only store the parent as an object, eg:

public class MyObj {
public Guid ID;
public string Label;
public MyObj Parent;

Then do:
<%= form.Select1(x => x.Parent, Model.ListOfParentMyObjs, x => x.Guid, x => x.Label) %>

I think you are trying to do this (which won't compile):

public class MyObj {
public Guid ID;
public string Label;
public Guid ParentGuid;

<%= form.Select1(x => x.ParentGuid, Model.ListOfParentMyObjs, x => x.Guid, x => x.Label) %>

I think its best practise to have object relationships and not just use the identifier. However, there are probably some cases where this is justified. I could change the first lambda argument from Func<TModelContext, T> to Func<TModelContext, object> to make the Select1 a bit more flexible. This would mean you could do it your way and the object way would change to:

<%= form.Select1(x => x.Parent.Guid, Model.ListOfParentMyObjs, x => x.Guid, x => x.Label) %>

What do you think?

sibbl wrote Apr 2, 2009 at 6:15 PM

It just won't work with your first method over here:

public class TaskGroup {
public Guid Guid;
public string Label;
public TaskGroup Parent;

public class TaskGroupViewData {
List<TaskGroup> ExistingTasks;

<%= form.Select1(c => c.Parent, Model.ExistingTasks, c => c.Guid, c => c.Label) %>

the second is too complicated for me ;) I'm not a professional C# developer, and I even didn't understand the hole tree of your project.

JontyMC wrote Apr 8, 2009 at 4:05 PM

What you have should work. What error are you getting? Have you checked the list of taskgroups are properly populated?

wrote May 5, 2009 at 9:20 AM

wrote Feb 13, 2013 at 11:40 PM

wrote May 16, 2013 at 6:12 AM