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

[Solved] Problem In MVC3 Telerik Grid with DropDownList

0 Answers 33 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.
Dfdfdfd
Top achievements
Rank 1
Dfdfdfd asked on 12 Jan 2012, 07:25 AM
Hi,
I am Fresher in MVC3 Telerik Grid
I want to create an Teletik mvc3 grid with dropdownlist and successfully created.
But problem data can not display in dropdownlist cell and can not data pass into the controller after submit.
Plz help.
Model:
public class Report
    {
        GetConnectionString _getString = new GetConnectionString();
        public Report()
        {
           
        }
        public int RecordId { get; set; }

        [UIHint("Member"), Required]
        [Display(Name = "MemName")]
        //public string Emp { get; set; }
        public Employee Emp { get; set; }
        //public int Emp { get; set; }
        

        [Required]
        [Display(Name = "Purpose:")]
        public string Purpose { get; set; }

        [Required]
        [Display(Name = "Amount:")]
        public double Amount { get; set; }

        [Required]
        [Display(Name = "Date:")]
        [DataType(DataType.Date)]
        [DateRange(ErrorMessage = "'{0}' {1} must be smaller then current date {2}.")]
        [DisplayFormat(DataFormatString = "dd/MM/yyyy")]
        public DateTime Date { get; set; }

        public string SubmittedBy { get; set; }

        public DateTime? SubmissionDate { get; set; }

        public string Remark { get; set; }
    }
    public class Employee
    {
        public Employee()
        {
        }
        public int MemId { get; set; }
        public string MemName { get; set; }
    }
Controller:
public class ReportController : Controller
    {
        //
        // GET: /Report/
        GetConnectionString _getString = new GetConnectionString();
        public ActionResult Reports()
        {
            ViewData["employees"] = (IEnumerable<Employee>)new MMDataModelDataContext().tblMemberInfos.Select(e =>
                                                new Employee { MemId = e.MemId, MemName = e.MemberNickName });
            return View();
        }
        public ActionResult DataList()
        {
            List<Employee> ItemList;
            using (MMDataModelDataContext _db = new MMDataModelDataContext(_getString.GetSQLString()))
            {
                ItemList = (from c in _db.GetTable<tblMemberInfo>()
                            select new Employee
                            {
                                MemId = int.Parse(c.MemId.ToString()),
                                MemName = c.MemberNickName
                            }
                           ).ToList<Employee>();
            }
            return new JsonResult { Data = new SelectList((IEnumerable<Employee>)ItemList, "MemId", "MemName", "MemName") };
        }

        [GridAction]
        public ActionResult Load()
        {
            return new JsonResult { Data =new GridModel(this.GridData())};
        }
        private IQueryable<Report> GridData()
        {
            return (IQueryable<Report>)new MMDataModelDataContext().v_CostEntries.Select(e => new Report
            {
                Emp = new Employee{ MemId=e.MemId,MemName=e.MemberNickName },
                //Emp = e.MemberNickName,
                //Emp=e.MemId,
                Purpose=e.Purpose,
                Amount=e.Amount,
                Date=e.Date,
                RecordId=e.RecordId,
                SubmissionDate=e.SubmissionDate,
                SubmittedBy=e.SubmittedBy
            });
        }
        [GridAction]
        public ActionResult Update(int id, Report emp)
        {
            return View();
        }
        [GridAction]
        public ActionResult Delete(int id, Report emp)
        {
            return View();
        }
        [GridAction]
        public ActionResult Insert(int id, Report emp)
        {
            return View();
        }
    }
View:
Report.cshtml:
@using Telerik.Web.Mvc.UI;
@using MessManagement.Models;

@(
 Html.Telerik().Grid<Report>()
     .DataKeys(datakeys => datakeys.Add(o => o.RecordId))
     .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.ImageAndText))
        .Name("grid")
        .Columns(columns =>
            {
                columns.Bound(o => o.RecordId).Hidden().HeaderTemplate("RecordId").Sortable(true);
                columns.Bound(o => o.Emp).HeaderTemplate("Member Name");
                columns.Bound(o => o.Purpose).HeaderTemplate("Purpose");
                columns.Bound(o => o.Amount).HeaderTemplate("Amount").Format("{0:n3}");
                columns.Bound(o => o.Date).HeaderTemplate("Date").Format("{0:dd/MM/yyyy}");
                columns.Bound(o => o.SubmittedBy).HeaderTemplate("Submitted By");
                columns.Bound(o => o.SubmissionDate).HeaderTemplate("Submission DateTime");
                columns.Command(commands => commands.Edit().ButtonType(GridButtonType.BareImage));
                columns.Command(commands => commands.Delete().ButtonType(GridButtonType.BareImage));
            })
             .DataBinding(dataBinding => dataBinding
                //Ajax binding
                .Ajax()
                //Home.Index renders the grid initially
                .Select("Load", "Report")
                .Update("Update", "Report")
                .Delete("Delete", "Report")
                .Insert("Insert", "Report")
            )
        .Editable(editing =>editing.DisplayDeleteConfirmation(true))
        .Selectable()
        .Pageable()
        .Sortable()
        .Filterable()
        .Groupable()
    )
Member.cshtml:
@model Employee
@using System.Collections;
@using Telerik.Web.Mvc.UI;
@using MessManagement.Models;
@(Html.Telerik().DropDownList()
        .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
                    .BindTo(new SelectList((IEnumerable)ViewData["employees"], "MemId", "MemName"))
)


Tags
Grid
Asked by
Dfdfdfd
Top achievements
Rank 1
Share this question
or