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

[Solved] Problem binding dropdown list in grid

2 Answers 76 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.
Jeff
Top achievements
Rank 1
Jeff asked on 23 Sep 2011, 01:51 PM
Hello all.  My current project uses a number of dropdown lists in the row editor for it's grids.  The method we normally use is to populate a SelectList in the Controller Action in question, assign it to the ViewBag, then populate an editor template with that and bind it to a variable using UIHint in the object ViewModel.  For one dropdown list in particular this works correctly in my dev environment, but after deploying the project it seems that it can't find the template and just shows up as a text box in edit mode.  The problem is occuring with the ProgramSubprogramID field.  Here is my code.

View
@(Html.Telerik().ScriptRegistrar().DefaultGroup(group =>                   
                    group.Add("jquery.validate.js").Add("jquery.validate.unobtrusive.js")))
  
        @(Html.Telerik().Grid<NGBears.ViewModels.SpendPlanProjectDetailViewModel>()
            .Name("SpendPlanGrid")
            .DataKeys(keys =>
            {
                keys.Add(sp => sp.SpendPlanProjectDetailID).RouteKey("SpendPlanProjectDetailID");
                keys.Add(sp => sp.LineTotal).RouteKey("LineTotal");
                keys.Add(sp => sp.Locked).RouteKey("Locked");
                keys.Add(sp => sp.SpendPlanProjectID).RouteKey("SpendPlanProjectID");
                keys.Add(sp => sp.DateCreated).RouteKey("DateCreated");
                keys.Add(sp => sp.FundedAccountID).RouteKey("FundedAccountID");
            })
            .ToolBar(commands =>
            {
                commands.Insert().ButtonType(GridButtonType.ImageAndText);
  
            })
            .DataBinding(dataBinding =>
            {
                dataBinding.Ajax()
                    .Select("_spendPlanProjectDetailSelect", "SpendPlan", new { spendPlanProjectID = Model.SpendPlanProjectID })
                    .Update("_spendPlanProjectDetailUpdate", "SpendPlan")
                    .Insert("_spendPlanProjectDetailInsert", "SpendPlan", new { ispendPlanProjectID = Model.SpendPlanProjectID });
            })
              
            .Columns(columns =>
            {
                columns.Command(commands => { commands.Edit().ButtonType(GridButtonType.Image); }).Title("Edit").Width(50);
                columns.Bound(sp => sp.SpendPlanProjectDetailID).ReadOnly().Title("Copy").Width(50).ClientTemplate("<a class='t-button t-button-icon copyRow' title='Copy Row'><span class='copyIcon'></span></a>");
                  
                columns.Bound(sp => sp.ColorOfMoneyID).ClientTemplate("<#= ColorOfMoneyColorOfMoneyDescription#>").Width(175);
                columns.Bound(sp => sp.ProgramSubprogramID).ClientTemplate("<#= ProgramSubprogramSubprogramSubprogramName#>" + " - " + "<#= ProgramSubprogramProgramProgramName#>").Width(250);
                columns.Bound(sp => sp.CostCodeID).Width(155);
                columns.Bound(sp => sp.CompanyID).Width(75).ClientTemplate("<#= CompanyCompanyName#>");
                columns.Bound(sp => sp.FundedAccountID).Title("Funded Account").ClientTemplate("<#= FundedAccountFundedAccountNumber #>").Width(110);
                columns.Bound(sp => sp.Qtr1Amt).Format("{0:c}").Width(135);
                columns.Bound(sp => sp.Qtr2Amt).Format("{0:c}").Width(135);
                columns.Bound(sp => sp.Qtr3Amt).Format("{0:c}").Width(135);
                columns.Bound(sp => sp.Qtr4Amt).Format("{0:c}").Width(135);
                columns.Bound(sp => sp.LineTotal).Format("{0:c}").Width(135).ReadOnly();
            })
            .ClientEvents(events => 
            {
                events.OnDataBound("GridDataBound");
                events.OnRowDataBound("GridRowDataBound");
            })
            .Editable(editing => editing.Mode(GridEditMode.InLine))
            .Scrollable(scrollable => scrollable.Height(500))
            .Pageable(paging => paging.Enabled(true).Style(GridPagerStyles.NextPrevious | GridPagerStyles.Numeric | GridPagerStyles.PageSizeDropDown | GridPagerStyles.Status))
            .Sortable()
        )

Controller
[OutputCache(CacheProfile = "ZeroCache")]
        [CustomAuthorize(Roles = "Admin, BudgetReps")]
        public ActionResult Details(int id)
        {
            var model = _spendPlanRepo.GetSpendPlanProject(id);
             
            SpendPlanProjectViewModel viewModel = Mapper.Map<SpendPlanProject, SpendPlanProjectViewModel>(model);
  
            //comments
            var comments = _spendPlanRepo.GetCommentsForSpendPlanProject(id);
            viewModel.SpendPlanProjectComments = Mapper.Map<IEnumerable<SpendPlanProjectComment>, IEnumerable<SpendPlanProjectCommentViewModel>>(comments);
  
  
            //subprograms
            var subprograms =_spendPlanRepo.GetAllProgramSubprograms();
            //IEnumerable<ProgramSubprogramViewModel> programSubprogramViewModel = Mapper.Map<IEnumerable<ProgramSubprogram>, IEnumerable<ProgramSubprogramViewModel>>(subprograms);
            List<SelectListItem> psSelectList = new SelectList(subprograms, "ProgramSubprogramID", "ProgramSubprogramID").ToList();
            IEnumerable<SelectListItem> psItems = psSelectList.AsEnumerable();
            ViewBag.ProgramSubprograms = psItems;
  
            //Color of Money
            var moneyColors = _spendPlanRepo.GetAllColorsOfMoney();
            List<SelectListItem> mcSelectList = new SelectList(moneyColors, "ColorOfMoneyID", "ColorOfMoneyDescription").ToList();
            mcSelectList.Insert(0, (new SelectListItem { Text = "", Value = "" }));
            IEnumerable<SelectListItem> mcItems = mcSelectList.AsEnumerable();
            ViewBag.ColorsOfMoney = mcItems;
  
            //cost codes
            var costCodes = _spendPlanRepo.GetAllCostCodes();
            List<SelectListItem> ccSelectList = new SelectList(costCodes, "CostCodeID", "CostCodeID").ToList();
            ccSelectList.Insert(0, (new SelectListItem { Text = "", Value = "" }));
            IEnumerable<SelectListItem> ccItems = ccSelectList.AsEnumerable();
            ViewBag.CostCodes = ccItems;
  
            //funding sources
            var fundedAccounts = _spendPlanRepo.GetAllFundedAccounts();
            List<SelectListItem> faSelectList = new SelectList(fundedAccounts, "FundedAccountID", "FundedAccountNumber").ToList();
            faSelectList.Insert(0, (new SelectListItem { Text = "", Value = "" }));
            IEnumerable<SelectListItem> faItems = faSelectList.AsEnumerable();
            ViewBag.FundedAccounts = faItems;
  
            //Companies
            var companies = _spendPlanRepo.GetAllCompanies();
            List<SelectListItem> coSelectList = new SelectList(companies, "CompanyID", "CompanyName").ToList();
            coSelectList.Insert(0, (new SelectListItem { Text = "", Value = "" }));
            IEnumerable<SelectListItem> coItems = coSelectList.AsEnumerable();
            ViewBag.Companies = coItems;
             
            return View(viewModel);
        }

Editor Template
@using Telerik.Web.Mvc.UI;
  
@(
    Html.Telerik().DropDownList()
      .Name("ProgramSubprogramID")
          .BindTo(ViewBag.ProgramSubprograms).HtmlAttributes(new { style = "width:100%;" })
 )

View Model
public class SpendPlanProjectDetailViewModel
    {
        public int SpendPlanProjectDetailID { get; set; }
         
        public int SpendPlanProjectID { get; set; }
  
        [UIHint("ProgramSubprogram"), Required]
        [DisplayName("Subprogram")]
        public int ProgramSubprogramID { get; set; }
  
        [DisplayName("Subprogram")]
        public string ProgramSubprogramSubprogramSubprogramName { get; set; } //Subprogram name
  
        [DisplayName("Program")]
        public string ProgramSubprogramProgramProgramName { get; set; } //Program name
             
        [DisplayName("Subprogram %")]
        public decimal SubProgramPercentage { get; set; }
  
        [DataType(DataType.Currency)]
        [DisplayName("Line Total")]
        [UIHint("LineTotal"), Required]
        public decimal LineTotal { get; set; }
         
        [Required]
        [Range(-999999999.00, 999999999.00)]
        [DataType(DataType.Currency)]
        [DisplayName("Qtr 1 Amt")]
        public decimal Qtr1Amt { get; set; }
  
        [Required]
        [Range(-999999999.00, 999999999.00)]
        [DataType(DataType.Currency)]
        [DisplayName("Qtr 2 Amt")]
        public decimal Qtr2Amt { get; set; }
  
        [Required]
        [Range(-999999999.00, 999999999.00)]
        [DataType(DataType.Currency)]
        [DisplayName("Qtr 3 Amt")]
        public decimal Qtr3Amt { get; set; }
  
        [Required]
        [Range(-999999999.00, 999999999.00)]
        [DataType(DataType.Currency)]
        [DisplayName("Qtr 4 Amt")]
        public decimal Qtr4Amt { get; set; }
  
        [UIHint("ColorOfMoney"), Required]
        [DisplayName("Color Of Money")]
        public int ColorOfMoneyID { get; set; }
  
        [DisplayName("Color Of Money")]
        public string ColorOfMoneyColorOfMoneyDescription { get; set; }
  
  
        [UIHint("FundedAccount"), Required]
        [DisplayName("Funded Account")]
        public int FundedAccountID { get; set; }
        public string FundedAccountFundedAccountNumber { get; set; }
  
        public int MoneyTypeID { get; set; }        
  
        [UIHint("CostCode"), Required]
        [DisplayName("Funded Cost Code")]
        public string CostCodeID { get; set; }
  
        [DisplayName("Cost Code")]
        public string CostCodeCostCodeDescription { get; set; }
  
        [UIHint("Company"), Required]
        [DisplayName("Company")]
        public int CompanyID { get; set; }
  
        [DisplayName("Company")]
        public string CompanyCompanyName { get; set; }
  
  
        public bool Locked { get; set; }
  
        public DateTime DateCreated { get; set; }
          
        public Guid CreatedBy { get; set; }
  
  
        //project details
        [DisplayName("Project Number")]
        public string SpendPlanProjectProjectDetailProjectProjectNumber { get; set; }
  
        [DisplayName("Project Name")]
        public string SpendPlanProjectProjectDetailProjectProjectName { get; set; }
  
        public string DropDownDisplay { get; set; }
    }

Like I said earlier, the problem occurs with the ProgramSubprogram drop down.  It works 100% correctly in my dev environment, but after deploying it the drop down menu breaks and I just get a text box in the editor.  Thanks for any help.

2 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 26 Sep 2011, 08:56 PM
Just an update.  I've narrowed the problem down to the EditorTemplate, though why it isn't working I have no clue.  Changing the data I'm feeding into the ProgramSubprograms ViewBag variable doesn't fix my problem, the field still appears as a textbox when the row goes to edit mode.  Binding my ProgramSubprogramID variable to a different EditorTemplate causes the drop down to appear normally with that template's data in it.  So the problem lies somewhere between when the data gets bound to the ViewBag variable and the ProgramSubprogram EditorTemplate gets instantiated, but I've run out of ideas for how to debug/test it at this point.  Thanks again for any advice.
0
Jeff
Top achievements
Rank 1
answered on 27 Sep 2011, 03:06 PM
I've resolved the issue.  It turns out the Editor Template file itself was corrupt in some way.  Deleting the file and remaking it with identical content resolved the issue.
Tags
Grid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Share this question
or