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

[Solved] MVC Grid ForeignKey setting a Default

10 Answers 461 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.
Deepak
Top achievements
Rank 1
Deepak asked on 15 Mar 2012, 04:52 AM
Hello all,

I'm using a ForeignKey column in an MVC Grid.  Can someone please tell me how best to set a default value when adding a new row?  I tried implementing the onEdit() method.  However, for some reason, the value in the ComboBox shows up ONLY when I click on the ComboBox.  I'd like to see the value immediately as soon as the row is added.

Any thoughts?

Deepak.

10 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 15 Mar 2012, 12:39 PM
hi,

Do you try to make the the Selected property from the SelectListItem to True in you controller?

var list = new List<SelectListItem>();
list.Add(new SelectListItem{Text="Test1", Value="1", Selected = true});
list.Add(new SelectListItem{Text="Test2", Value="2"}); 

ViewBag.MyList = list;

 Html.Telerik().Grid>ClientEditableOrder>()
           
.Name("Grid")                      
           
.Columns(columns =>
           
{
               columns
.Bound(o => o.OrderID).Width(100);
               
columns.ForeignKey(o => o.EmployeeID, (IEnumerable)ViewBag.MyList,
                   
"ID", "Name")
.Width(230);
               columns
.Bound(o => o.OrderDate).Width(150);
               columns
.Bound(o => o.Freight).Width(220);              
           
})                      

0
Deepak
Top achievements
Rank 1
answered on 15 Mar 2012, 12:53 PM
Thanks for your response.  Unfortunately, it did not work.
0
Accepted
Dadv
Top achievements
Rank 1
answered on 15 Mar 2012, 05:15 PM
i try and it simple really :

you just have to add this :

 .Editable(e=>e.DefaultDataItem(new MyModel{TheForeignKeyId=[TheDefaultForeignKeyValue]}))


for example :

 Html.Telerik().Grid(Model)
.Name("Grid")
.DataKeys(keys => keys.Add(c => c.ProductID))
.ToolBar(commands => commands.Insert().ButtonType(type).ImageHtmlAttributes(new { style = "margin-left:0" }))
.DataBinding(dataBinding => dataBinding.Server()
.Select("EditingServerSide", "Grid", new { mode = mode, type = type })
.Insert("Insert", "Grid", new { mode = mode, type = type })
.Update("Save", "Grid", new { mode = mode, type = type })
.Delete("Delete", "Grid", new { mode = mode, type = type }))
.Columns(columns =>
{
columns.Bound(p => p.ProductName).Width(210);
columns.Bound(p => p.UnitPrice).Width(120).Format("{0:c}");
columns.Bound(p => p.UnitsInStock).Width(120);
columns.Bound(p => p.LastSupply).Width(120).Format("{0:d}");
            columns.Bound(p => p.Discontinued);
            columns.ForeignKey(p => p.EmployeeID, (SelectList)ViewData["employees"]).Width(230);
columns.Command(commands =>
{
commands.Edit().ButtonType(type);
commands.Delete().ButtonType(type);
}).Width(200).Title("Commands");
})
                .Editable(e=>e.DefaultDataItem(new EditableProduct{EmployeeID=2}))
.Editable(editing => editing.Mode(mode))
.Pageable()
.Sortable()

(work in server and ajax mode)
0
Deepak
Top achievements
Rank 1
answered on 15 Mar 2012, 07:04 PM
This worked.

Thanks!
0
Dadv
Top achievements
Rank 1
answered on 16 Mar 2012, 09:17 AM
please don't forget to mark as answer
0
Dipak
Top achievements
Rank 1
answered on 16 Mar 2012, 07:11 PM

 Controller
=========

 

public ViewResult Index()
       {
          List<Job> data = _JobRepo.GetAll().ToList<Job>();
           IEnumerable datanew = GetData(data, new GridCommand());
 
           ViewData["total"] = data.Count();
           //ViewData["Clients"] = new SelectList(db.Client, "ClientID", "ClientName");
           ViewBag.JobOwner = new SelectList(db.User.AsEnumerable<User>(), "UserID", "LoginName");
 
           return View(datanew);
       }

 

 

 Error
=======

 

DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'UserID'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'UserID'.

Source Error:

Line 10: <%: Html.ActionLink("Create New", "Create") %> Line 11: </p> Line 12: <%= Html.Telerik().Grid(Model) Line 13: .Name("Grid") Line 14: .DataKeys(keys => { keys.Add(o => o.JobID); })

 

View
======

 

.Columns(columns =>
                               {
                                   columns.Bound(o => o.JobNumber).Width(100);
                                   columns.Bound(o => o.JobOwnerName).Width(100).Sortable(false).Filterable(false).Groupable(false);
                                   columns.Bound(o => o.ClientName).Width(100);
                                   //columns.ForeignKey(o => o.ClientID, (IEnumerable)ViewData["Clients"],"ClientID","ClientName");
                                   columns.ForeignKey(o => o.JobOwner, (IEnumerable)ViewData["JobOwner"], "UserID", "LoginName");
                                   columns.Command(commands =>
                                   {
                                       commands.Edit().ButtonType(GridButtonType.ImageAndText);
                                   }).Width(200).Title("Commands");
                               })

Can you please look into this?

0
Dadv
Top achievements
Rank 1
answered on 21 Mar 2012, 10:51 AM
Hi,

the problem is here : 

columns.ForeignKey(o => o.JobOwner, (IEnumerable)ViewData["JobOwner"], "UserID", "LoginName");
 

your IEnumerable is a  SelectList

replace by that : 

columns.ForeignKey(o => o.JobOwner, (SelectList)ViewData["JobOwner"]); 


0
Dipak
Top achievements
Rank 1
answered on 21 Mar 2012, 03:31 PM
Thanks for your reply. Its works for me. However i caught in another problem after that. In Edit in "Inline" its shows me values like "False False". and in PopUp it shows me Id instead of DropDown.

Do you have any working code for this?

Thanks
0
Dadv
Top achievements
Rank 1
answered on 21 Mar 2012, 05:58 PM
Try this :

columns.ForeignKey(o => o.JobOwner, (IEnumerable)ViewData["JobOwner"], "Value", "Text");  

but i don't think this will change anything...

I don't know why you don't have the dropdown.
0
Zio
Top achievements
Rank 1
answered on 07 Sep 2012, 09:38 AM
Hello
what about the foreignkey column for a grid without cast? I mean, my grid is not casted to a mode, I just use the dt (datable) such:

Html.Telerik().Grid(dt)
                    .Name(vd.Name + "_grid")
                    .HtmlAttributes(new { @class = "telerikGrid" })
                    .Columns(column =>

How can I use the ForeignKey on that grid?

thanks
Tags
Grid
Asked by
Deepak
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Deepak
Top achievements
Rank 1
Dipak
Top achievements
Rank 1
Zio
Top achievements
Rank 1
Share this question
or