This question is locked. New answers and comments are not allowed.
Seems this should be pretty simple. Each custodian should have a default SecurityIDType assigned to them. I want the SecurityIDType to be a picklist in the grid when inserting or editing. I've included what I believe to be all the related code below. If someone can help me figure out why the DropDownList never shows up, that would be great.
All of the binding and editing features seem to work, otherwise.
Thanks.
J
View Models:
View (Index.cshtml)
Editor Template (SecurityIDTypePicklist.cshtml)
Controller (CustodianController.cs)
All of the binding and editing features seem to work, otherwise.
Thanks.
J
View Models:
public class CustodianViewModel{ [ReadOnly(true)] public int ID { get; set; } [Required] public string Description { get; set; } public bool Deleted { get; set; } [UIHint("SecurityIDTypePicklist"), Required] public SecurityIDTypeViewModel DefaultSecurityIDType { get; set; }}public class SecurityIDTypeViewModel{ public int ID { get; set; } public string Description { get; set; } public bool Deleted { get; set; }}View (Index.cshtml)
@model IEnumerable<CamBakWeb.ViewModels.CustodianViewModel>@{ Layout = "~/Views/Shared/_LayoutTwoColumns.cshtml"; }<div class="paneheader">Manage Custodians</div>@(Html.Telerik().Grid <CustodianViewModel>() .Name("Grid") .Sortable(sorting => sorting.OrderBy(sortOrder => sortOrder.Add(o => o.Description))) .Pageable(paging => paging.PageSize(20)) .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Image)) .DataKeys(keys => keys.Add(o => o.ID)) .DataBinding(dataBinding => dataBinding.Ajax() .Select("Index", "Custodian") .Insert("Insert", "Custodian") .Update("Update", "Custodian") .Delete("Delete", "Custodian")) .Columns(columns => { columns.Bound(o => o.Description); columns.Bound(o => o.DefaultSecurityIDType.Description).Title("Default SID Type").Width(200); columns.Command(commands => { commands.Edit().ButtonType(GridButtonType.Image); commands.Delete().ButtonType(GridButtonType.Image); }).Width(76); }))Editor Template (SecurityIDTypePicklist.cshtml)
@(Html.Telerik().DropDownList() .Name("DefaultSecurityIDType.Description") .BindTo(new SelectList((System.Collections.IEnumerable)ViewBag.SecurityIDTypes, "Id", "Description")))Controller (CustodianController.cs)
using System;using System.Collections.Generic;using System.Data;using System.Data.Entity;using System.Linq;using System.Web;using System.Web.Mvc;using CamBakWeb.Models;using CamBakWeb.ViewModels;using Telerik.Web.Mvc;namespace CamBakWeb.Controllers{ public class CustodianController : Controller { private CamBakWebDB db = new CamBakWebDB(); public ViewResult Index() { return View(GetActiveRecords()); } [ActionName("Index")] [AcceptVerbs(HttpVerbs.Post)] [GridAction] public ViewResult _Index() { return View(new GridModel(GetActiveRecords())); } [AcceptVerbs(HttpVerbs.Post)] [GridAction] public ActionResult Insert() { Custodian custodian = new Custodian(); if (TryUpdateModel(custodian)) { db.Custodians.Add(custodian); db.SaveChanges(); } return View(new GridModel(GetActiveRecords())); } [AcceptVerbs(HttpVerbs.Post)] [GridAction] public ActionResult Update(int id) { Custodian custodian = db.Custodians.Single(o => o.ID == id); if (TryUpdateModel(custodian)) { db.SaveChanges(); } return View(new GridModel(GetActiveRecords())); } [AcceptVerbs(HttpVerbs.Post)] [GridAction] public ActionResult Delete(int id) { Custodian custodian = db.Custodians.Single(o => o.ID == id); if (custodian != null) { custodian.Deleted = true; db.SaveChanges(); } return View(new GridModel(GetActiveRecords())); } private IEnumerable<CustodianViewModel> GetActiveRecords() { var custodians = db.Custodians.Where(o => o.Deleted != true) .Select(o => new CustodianViewModel { ID = o.ID, Description = o.Description, DefaultSecurityIDType = new SecurityIDTypeViewModel { ID = o.DefaultSecurityIDType.ID, Description = o.DefaultSecurityIDType.Description, Deleted = o.DefaultSecurityIDType.Deleted }, Deleted = o.Deleted }).ToList(); var types = db.SecurityIDTypes .Select(o => new SecurityIDTypeViewModel { ID = o.ID, Description = o.Description, Deleted = o.Deleted }).ToList(); ViewBag.SecurityIDTypes = types; return custodians; } }}