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

[Solved] BUG - Grid ForeignKey Columns (and EditorTemplates?)

5 Answers 148 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.
Rajesh
Top achievements
Rank 1
Rajesh asked on 23 Mar 2012, 05:05 AM
Hi,

I have a PartialView with multiple grids. Each grid is bound to a different table. However, all these tables have a foreign key reference to the same table, and the id for this foreign key column is the same.

I use Coulmns.ForeignKey to bind the grid column. At runtime, i get an error that says the key has already been added.

The only possible workarounds i could find are -- change the field name in the database(this worked, or define new ViewModels that have different IDs for the foreign key.

Hope this will be fixed in the coming releases.


Grid1, Bound to Table 1 --> ForeignKey(MyFK)
Grid2, Bound to Table 2 --> ForeignKey(MyFK)

This needs to change to 

Grid1, Bound to Table1 --> ForeignKey(MyFKTable1)
Grid2, Bound to Table2 --> ForeignKey(MyFKTable2)

Till the time there is a fix... one needs to be aware of this when designing the database!!!

5 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 23 Mar 2012, 09:15 AM
Hi,

What do you use for Entities? Hibernate? other Poco ?

This seem to not be a Telerik problem, but a entities one
0
Rajesh
Top achievements
Rank 1
answered on 24 Mar 2012, 05:39 AM
Am using Entity Framework 4.0.. Certainly a grid problem... the entity framework has nothing to do with how telerik manages editor templates for multiple grids with the same foreign key name.

Perhaps the best workaround is to define a new property in partial model class with a unique name across the universe!
0
Dadv
Top achievements
Rank 1
answered on 26 Mar 2012, 08:26 AM
I was only say that because a lot of time in this forum when people see "key has already been added" , it was a Entities problem, not a grid problem. But your case is probably different.

Other thing is that if you add 2 time the same foreignkey in the same grid, you don't get any error (got a grid with this for 2 different display).

Can you give us some of your grid code or a sample project to see the problem in action ?

Regards,
0
Dipak
Top achievements
Rank 1
answered on 24 Apr 2012, 02:46 PM
Hey Rajesh,

I have similiar kind of problem. I have two fields in the table that contains foriegn key ref with the same table. using foriegn key column the first dropdown fields perfect but in the second i got error message "The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'Question'.

I have below Model in my code. I have tried with two seperate Editor Template for two column but it didn't work. i tried with the same Template but no luck. if i remove "UIHint" from second column then it is working but i am not getting dropdown in Add/Edit mode.

Thanks in advanced.
public partial class EventLinkedRule
    {
        [Key, Column(Order = 0)]
        public int RuleID { get; set; }
 
        [Key, Column(Order = 1)]
        [UIHint("ClientDropDown")]
        [Required(ErrorMessage = "Please Select Client.")]
        [Display(Name = "Client Name")]
        public int ClientId { get; set; }
 
        public string RuleName { get; set; }
 
        [Key, Column(Order = 2)]
        [UIHint("QuestionDropDown")]
        [Required(ErrorMessage = "Please Select Question.")]
        [Display(Name = "Question")]
        public int QuestionId { get; set; }
 
        //[Key, Column(Order = 3)]
        [UIHint("QuestionDropDown")]
        [ForeignKey("Question")]
        [Required(ErrorMessage = "Please Select Question.")]
        [Display(Name = "Dependent Question")]
        public int DependentQuestionId { get; set; }
 
        [Display(Name = "Operator")]
        public string Operator { get; set; }
 
        [Display(Name = "No of Days")]
        public int Operand { get; set; }
 
        [Display(Name = "Active")]
        public bool IsActive { get; set; }
 
        [ScaffoldColumn(false)]
        public Nullable<int> CreatedByUser { get; set; }
        [ScaffoldColumn(false)]
        public Nullable<System.DateTime> CreatedDate { get; set; }
        [ScaffoldColumn(false)]
        public Nullable<int> ModifiedByUser { get; set; }
        [ScaffoldColumn(false)]
        public Nullable<System.DateTime> ModifiedDate { get; set; }
 
        //public virtual Client Client { get; set; }
        //public virtual Question Question { get; set; }
        public virtual Question Question { get; set; }
    }

0
Rajesh
Top achievements
Rank 1
answered on 25 Apr 2012, 02:27 PM
Dipak,

In the code you have pasted there is only one column marked with the ForeignKey attribute.

The problem i mentioned is multiple grids, and each has a foreign key with the same name. The workaround for that was simple -- i changed the names of my fields so that they are unique (can be done in ViewModel too).

I do have a grid, where all the 5 properties are foreign keys to the same table -- and that works fine (as obviously the property names are different because they belong to the same table).

However, i am not using ViewModels. 
Tags
Grid
Asked by
Rajesh
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Rajesh
Top achievements
Rank 1
Dipak
Top achievements
Rank 1
Share this question
or