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

Want to populate other columns in the grid from a singel DropDownList using ViewData

1 Answer 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Larry
Top achievements
Rank 1
Larry asked on 03 Sep 2014, 03:37 PM
I have a grid that has multiple DropDownLists.   All of the DropDownLists are populated using the ViewData object.    In this case, I have a DropDownList that I would actually like it to populate multiple other columns in the Grid.    I am not sure how I can access the other information in the ViewData object.   So the Project_Key and the NameDesc columns in the model are for the dropdown.   But, the other columns Mgt_Unit_Key, Program_Key and Zone_Key are columns in the ViewData that I want to use to populate (default) other columns in the grid to.    How can I access these other columns in the ViewData and assign them to columns in the grid?

Here is the basics of my controller:
    public ActionResult TimecardIndex()
        {
            ViewData["Projects"] = this.projectservice.GetDropDownList();
            return View();
        }

The grid looks like:
@(Html.Kendo().Grid<IRISWeb.Models.CAS.Timecard>()
    .Name("Grid")
    .Columns(c =>
    {
        c.Command(command => { command.Destroy(); }).Width(120).Lockable(true).Locked(true);
        c.Bound(p => p.Task_Date).Width(120).Lockable(true).Locked(true).EditorTemplateName("IRISDate");
        c.ForeignKey(p => p.Activity_Key, (System.Collections.IEnumerable)ViewData["Activities"], "Activity_Key", "NameDesc").Width(350).Lockable(true);
        
        c.ForeignKey(p => p.Project_Key, (System.Collections.IEnumerable)ViewData["Projects"], "Project_Key", "NameDesc").Width(350);
        c.ForeignKey(p => p.ProjectSub_Key, (System.Collections.IEnumerable)ViewData["ProjectSubs"], "ProjectSub_Key", "NameDesc").EditorTemplateName("ProjectSub").Width(200);
        
        c.ForeignKey(p => p.Mgt_Unit_Key, (System.Collections.IEnumerable)ViewData["Mgt_Units"], "Mgt_Unit_Key", "NameDesc").Width(350);
        c.ForeignKey(p => p.Program_Key, (System.Collections.IEnumerable)ViewData["Programs"], "Program_Key", "NameDesc").Width(350);
        c.ForeignKey(p => p.Zone_Key, (System.Collections.IEnumerable)ViewData["Zones"], "Zone_Key", "NameDesc").Width(350);
        c.ForeignKey(p => p.Road_Key, (System.Collections.IEnumerable)ViewData["Roads"], "Road_Key", "FullRoadNumber").Width(200);
.....

The model for the ProjectDropDown returned by the GetDropDownList() is:
 public class ProjectDropDown
    {
        [Key]
        [ScaffoldColumn(false)]
        public string Project_Key { get; set; }

        [MaxLength(70)]
        [Display(Name = "Name")]
        public string NameDesc { get; set; }

        [MaxLength(10)]
        [Display(Name = "Mgt. Unit")]
        public string Mgt_Unit_Key { get; set; }

        [MaxLength(10)]
        [Display(Name = "Program")]
        public string Program_Key { get; set; }

        [MaxLength(10)]
        [Display(Name = "Zone")]
        public string Zone_Key { get; set; }

        [MaxLength(10)]
        [Display(Name = "Road")]
        public string Road_Key { get; set; }
    }





























































1 Answer, 1 is accepted

Sort by
0
Accepted
Alexander Popov
Telerik team
answered on 05 Sep 2014, 10:01 AM
Hello Larry,

I already addressed this query in the support ticket you opened, however I will post my reply here as well.

This could be acheived by subscribing the data item's change event and modifying the values accordingly. For example: 
@(Html.Kendo().Grid<Model>()
    .Name("grid")
    .Events(e=>e.Edit("onEdit"))
    ...
)
<script type="text/javascript">
    function onEdit(e) {
        e.model.bind("change", function (evt) {
            this.set("MyField", newValue); //get the value of the first DropdDwnList and use it to update the rest of the fields
        })
    }
</script>

Regards,
Alexander Popov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Larry
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or