This is a migrated thread and some comments may be shown as answers.

foreign key columns and null values

5 Answers 197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Milos
Top achievements
Rank 1
Milos asked on 27 Feb 2012, 05:51 PM
Hello guys.

How can i enter null value into my foreign key column when that column allows nulls, when using ForeignKey option of Telerik grid. I get my dropdown but i must always select one option. How can I avoid that?

Best regards.

5 Answers, 1 is accepted

Sort by
0
Ariel
Top achievements
Rank 1
answered on 08 Mar 2012, 04:40 PM
I am also having this issue. I'm wondering if nullable foreign keys are not yet supported or it's a bug. Haven't found anything in the docs about it.
0
MB
Top achievements
Rank 1
answered on 08 Mar 2012, 06:13 PM
Same here! I am struggling to find any help on this gridforeignkey topic. The demo is lacking any usable detail.
0
Milos
Top achievements
Rank 1
answered on 08 Mar 2012, 07:43 PM
I've created a runaround that works for me.

I'd usualy create foreignKey column like this:

c.ForeignKey(m => m.Account, new SelectList(ViewBag.Accounts as IEnumerable<Account>, "IdAccount", "AccountName"));

 

But, now, at the top of the view, i modify collection for foreignKey lookup, adding "NULL" value I need:

 

@{
List<SelectListItem> accounts = new SelectList(ViewBag.Accounts as IEnumerable<Account>, "IdAccount", "AccountName").ToList();
accounts.Insert(0, new SelectListItem { Text = "", Value = "" });
}

And, only after that I create foreign key column:

c.ForeignKey(m => m.Account, new SelectList(accounts, "Value", "Text"));

 

 

 

0
MB
Top achievements
Rank 1
answered on 08 Mar 2012, 08:52 PM
Thanks, Milos. I used your guidance to do this slightly differently for my scenario. Code placed here for others traveling this path. (I suggest bringing Scotch for this trip.)

I add the empty value in the controller when I populate the list. No changes needed in my view. In my example below, my lookup list is a "Profile" for an "AccountUser". This propulates the ViewData as per the MVC demo example.

   private void PopulateProfiles()
        {
            var profiles = Context.Profiles.Select(s => new { Oid = s.Oid, s.ProfileName }).OrderBy(s => s.ProfileName).ToList();  // list from DB
            profiles.Add(new { Oid = string.Empty, ProfileName = string.Empty });  // bogus empty one.
            ViewData["profiles"] = profiles;
        }
0
Milos
Top achievements
Rank 1
answered on 08 Mar 2012, 10:00 PM
Glad i could help :)
Tags
Grid
Asked by
Milos
Top achievements
Rank 1
Answers by
Ariel
Top achievements
Rank 1
MB
Top achievements
Rank 1
Milos
Top achievements
Rank 1
Share this question
or