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

[Solved] Assign Multiple Datakeys

14 Answers 557 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.
Won
Top achievements
Rank 1
Won asked on 09 Sep 2010, 05:15 PM
How do I assign multiple datakeys when there are 3 composite primary key?

I tried this way, but then only "COMPANY_NM" as a datakey.
 .DataKeys(k => k.Add(o => o.EXP_ID))
 .DataKeys(k => k.Add(o => o.EMP_ID))
 .DataKeys(k => k.Add(o => o.COMPANY_NM))

 Below is for onw primary key.

<% 
     Html.Telerik().Grid((List<Tiger.Models.EMP_EXPERIENCE>)ViewData["empExperience"])
            .Name("Grid")
            //.ToolBar(c => c.Insert())
            .DataKeys(k => k.Add(o => o.EXP_ID))
            .Columns(c =>
            {
                c.Bound(o => o.COMPANY_NM).ReadOnly().Title("Company Name").Width(200);
                c.Bound(o => o.DEPARTMENT_NM).ReadOnly().Title("Department").Width(200);
                c.Bound(o => o.POSITION_NM).ReadOnly().Title("Position").Width(200);
                c.Bound(o => o.START_DT).ReadOnly().Title("Hire Date").Format("{0:d}");
                c.Bound(o => o.END_DT).ReadOnly().Title("Retired Date").Format("{0:d}");
                c.Command(s =>
                {
                    s.Edit();
                    s.Delete();
                }).Width(200);
             })
             .DataBinding(b => b.Server()
                .Select("Edit", "Experience")
                .Insert("Create", "Experience")
                .Update("Edit", "Experience")
                .Delete("Delete", "Experience")
              )
            .Pageable()
            .Sortable()
            .Filterable()
            .Groupable()
            .Render();
     
      %>

14 Answers, 1 is accepted

Sort by
0
Nestor
Top achievements
Rank 1
answered on 02 Nov 2010, 01:12 PM

Try this way:

Html.Telerik().Grid(Model.ProjectDetails)
.Name("Grid")
.DataKeys(dataKeys => 
{
dataKeys.Add(m => m.ProjectID);
dataKeys.Add(m => m.PictureID);
}
)
.DataBinding(dataBinding => dataBinding.Server()
.Delete("DeleteProjectDetail", "Projects"))
Html.Telerik().Grid(Model.ProjectDetails)
    .Name("Grid")
    .DataKeys(dataKeys =>
    {
    dataKeys.Add(m => m.ProjectID);
    dataKeys.Add(m => m.PictureID);
    }
    )
    .DataBinding(...)...
0
Hernando
Top achievements
Rank 1
answered on 16 May 2011, 09:50 PM
tampoco funciona!! :(
0
erick
Top achievements
Rank 1
answered on 05 Jul 2011, 07:14 AM
any solution for this one??

thx,
erick
0
Atanas Korchev
Telerik team
answered on 05 Jul 2011, 07:27 AM
Hi,

 Multiple datakeys are assigned like this:

.DataKeys(dataKeys =>
    {
    dataKeys.Add(m => m.ProjectID);
    dataKeys.Add(m => m.PictureID).RouteKey("pictureId");
    }
    )

The only tricky thing is that you need to assign a unique RouteKey to the keys otherwise they will be send as "id".

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
erick
Top achievements
Rank 1
answered on 05 Jul 2011, 07:51 AM
thx Atanas Korchev for the reply

The only tricky thing is that you need to assign a unique RouteKey to the keys otherwise they will be send as "id". -> can u elaborate more on this??

the tricky part for me is when user editing the record in Master/Details grid,
ie: got 2 DataKeys(ProjectID, OwnerName) and several Columns(col1, col2, ...)

user only able to edit col1, col2, ... because the two DataKeys is for me to search in my records
also when adding a new one, User can input OwnerName, col1, col2, ... because the ProjectID is from the Master record..

any idea?
thx,
erick



0
Atanas Korchev
Telerik team
answered on 05 Jul 2011, 07:56 AM
Hi Erick,

If you have a single data key it is sent to the server as "id" unless you set its RouteKey.

However if you have more than one data key you must set the RouteKey of at least one of them. Here is how the action method would look like:

public ActionResult Save(int id, int pictureId)
{
}


Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
erick
Top achievements
Rank 1
answered on 05 Jul 2011, 08:21 AM
thx Korchev 

yes..it can be used like that (just found out now)

what if i want to pass the oldvalue for "id" and "pictureId" so i can retrieve it correctly before updating it?
thx,
erick
0
Atanas Korchev
Telerik team
answered on 05 Jul 2011, 08:29 AM
Hi Erick,

 What do you mean by "old value"? The grid will submit the current value of the data key.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
erick
Top achievements
Rank 1
answered on 05 Jul 2011, 08:38 AM
hm, let say i have master/detail records (Order - OrderLines)

in Order i have : OrderID as PK,
in OrderLines i have: OrderID, ProductID as PK which become FK to Order, Qty, Price, etc

i displayed it using Ajax Master/Detail grid and give the User ability to edit the Lines whenever User retrieve the OrderID from Database

the initial Order:
OrderID: 1
  • Line1: ProductID: 2, Qty: 10, Price: 20
  • Line2: ProductID: 3, Qty: 100, Price: 200

User edit the first line become:
  • Line1: ProductID: 4, Qty: 10, Price: 20

i must retrive the old Line1 record first from DB right (retrieve using OrderID, ProductID key) so i can edit its value to the new one?

thx,
erick


0
Atanas Korchev
Telerik team
answered on 05 Jul 2011, 08:43 AM
Hello Erick,

 You should retrieve the parent OrderID value from the parent dataItem. Then you can use the OnSave event to send that value to the server:

function onSave(e) {
    var parentGrid = $("#ParentGrid").data("tGrid");
    var parentRow = $(this).closest("tr");
    var parentDataItem = parentGrid.dataItem(parentRow);
   
    e.values.OrderID = parentDataItem.OrderID; // values is sent to the server.
}

Regards,

Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
erick
Top achievements
Rank 1
answered on 05 Jul 2011, 08:52 AM
o no..using jQuery? :(
no idea how to do that...

gonna try Server binding then

thx for the clarification Atanas Korchev, been trying for 2 days 

thx,
erick
0
Hernando
Top achievements
Rank 1
answered on 05 Jul 2011, 02:13 PM
Hi,

 Multiple datakeys are assigned like this:

//VIEW

.DataKeys(dataKeys =>
    {
    dataKeys.Add(m => m.ID).RouteKey("id");
    dataKeys.Add(m => m.TP).RouteKey("tp");
    dataKeys.Add(m => m.NAME).RouteKey("name");
    dataKeys.Add(m => m.LASTNAME).RouteKey("lastname");
    }
    )

//CONTROLLER
[HttpPost]
   public ActionResult Edit(int id, string tp, string name, string lastname, FormCollection formValues)
   {
   }

this way I have implemented up to 4 DataKeys and it has worked, hence the name of RouteKey ("id") should be receiving the same function in the controller.

I hope this solution to your liking.

Attentively
,
Hernando Lamprea
DCA TECHNOLOGY LTDA
www.dcatech.com
0
erick
Top achievements
Rank 1
answered on 06 Jul 2011, 02:19 AM
thx Hernando for the reply

i understand it will referencing in the controller as u said, but i can't update it since i give the ability for User to update the Details line
i think i need to add PrimaryKeys that can't be updated so i can use your and Atanas method

thx,
erick
0
Hetal
Top achievements
Rank 1
answered on 06 Dec 2012, 01:45 PM
Hey I have similar problem as you.

Did you create new PK? or you got any new solution ?
Tags
Grid
Asked by
Won
Top achievements
Rank 1
Answers by
Nestor
Top achievements
Rank 1
Hernando
Top achievements
Rank 1
erick
Top achievements
Rank 1
Atanas Korchev
Telerik team
Hetal
Top achievements
Rank 1
Share this question
or