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

[Solved] Can't Get Editor Template To Display

3 Answers 86 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jerry
Top achievements
Rank 1
Jerry asked on 03 Sep 2011, 08:24 AM
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:
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;
        }
    }
}

















3 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 03 Sep 2011, 03:47 PM
Hello Jerry,

 This sounds like the project described in this blog post.

All the best,
Atanas Korchev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Jerry
Top achievements
Rank 1
answered on 04 Sep 2011, 02:27 AM
Yes, it sounds like it, but I still can't get it to work.  I'm even trying to follow the code that appears to work in the demo solutions that's installed by the Telerik setup.  But there's so much going on that project with MVC being combined with ASPX that I'm obvioulsy not following it properly.  I find it hard to believe that there's not a simple example of how to make this work.  I've been searching for two days.

Any further advice would be appreicated.

Thanks.

Jerry
0
Atanas Korchev
Telerik team
answered on 05 Sep 2011, 07:30 AM
Hello Jerry,

 You probably need to create a custom editor template for your root view model as ASP.NET MVC will not invoke the child editor template out of the box (as described in the blog post). A sample project is attached to that very blog post and you can play with it.

You can also check the custom popup edit form code project which also uses as custom editor template.

Regards,
Atanas Korchev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
General Discussions
Asked by
Jerry
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Jerry
Top achievements
Rank 1
Share this question
or