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

Dropdown is not populating data

0 Answers 146 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kumar Bharat
Top achievements
Rank 1
Kumar Bharat asked on 05 May 2014, 08:39 AM
Hi,

dropdown is not populating data while inline edit of grid
Search.cshtml
-------------
@model TelerikMvc5App.ViewModel.EmployeeViewModel
@(Html.Kendo().Grid<TelerikMvc5App.ViewModel.EmployeeViewModel>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.EmployeeId).Width(50);
        columns.Bound(p => p.EmployeeName).Width(140);
        columns.Bound(p => p.DateOfBirth).Width(100);
        columns.Bound(p => p.GenderId).Width(100);
        columns.Bound(p => p.MariatalStatusId).Width(110).EditorTemplateName("_MariatalStatusPartial").ClientTemplate("#:MariatalStatus#");
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
    })
         .ToolBar(toolbar => toolbar.Create())
        .Editable(editable => editable.Mode(GridEditMode.InLine))
                  .Selectable(s => s.Enabled(true)
                                    .Type(GridSelectionType.Row)
                                    .Mode(GridSelectionMode.Single))
    .Pageable(pageable => pageable
                .Refresh(true)
                .PageSizes(true)
                .ButtonCount(5))
    .Navigatable()
    .Filterable()
    .Sortable()
    .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Batch(true)
        .ServerOperation(false)
            .Events(events => events.Error("error_handler"))        
                   .Model(model =>
                    {
                        model.Id(p => p.EmployeeId);
                        model.Field(p => p.EmployeeId).Editable(false);
                        model.Field(p => p.MariatalStatusId);
                    })

            .Create("CreateEmp", "Employee", new { Area = "Employee" })
            .Read("SearchEmp", "Employee", new { Area = "Employee" })
            .Update("SaveEmp", "Employee", new { Area = "Employee" })
            .Destroy("DeleteEmp", "Employee", new { Area = "Employee" })
    )
)
<script type="text/javascript">  

    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
</script>
--------------------
_MariatalStatusPartial.cshtml
----------------------------
@model TelerikMvc5App.ViewModel.EmployeeViewModel
@Html.Kendo().DropDownListFor(m => m.MariatalStatusId).DataTextField("Text").DataValueField("Value").DataSource(ds => ds.Read(r => { r.Action("GetMariatalStatus", "Employee", new { Area = "Employee" }); })).OptionLabel("Select Marital Status")


------------------
controller
-----------------------
 public ActionResult Search()
        {
            if (empList.Count == 0)
            {
                GetAllEmployees();
            }

            return View();
        }
 public JsonResult GetMariatalStatus()
        {
            List<SelectListItem> mariatalStatusList = new List<SelectListItem>();
            mariatalStatusList.Add(new SelectListItem { Text = "Single", Value = "1" });
            mariatalStatusList.Add(new SelectListItem { Text = "Married", Value = "2" });
            mariatalStatusList.Add(new SelectListItem { Text = "Others", Value = "3" });
            return Json(mariatalStatusList, JsonRequestBehavior.AllowGet);
        }

 private List<EmployeeViewModel> GetAllEmployees()
        {           
            empList.Add(new EmployeeViewModel() { EmployeeId = 1, EmployeeName = "Kumar", GenderId=1, MariatalStatusId=1 , MariatalStatus="Single" });
            empList.Add(new EmployeeViewModel() { EmployeeId = 2, EmployeeName = "Jaysankar", GenderId = 2, MariatalStatusId = 2, MariatalStatus = "Married" });
            empList.Add(new EmployeeViewModel() { EmployeeId = 3, EmployeeName = "Sathish" });
            empList.Add(new EmployeeViewModel() { EmployeeId = 4, EmployeeName = "Koushik" });
            empList.Add(new EmployeeViewModel() { EmployeeId = 5, EmployeeName = "Ramesh" });            
            return empList;
        }
------------

----------------
 public class EmployeeViewModel
    {
        public string EmployeeName { get; set; }
        public int? EmployeeId { get; set; }        
        
        public string LastName { get; set; }
        [Display(Name = "Gender")]
        [Required(ErrorMessage = "Gender is Required.")]
        public int GenderId { get; set; }
        [Display(Name = "Gender")]
        public string Gender { get; set; }

        [Display(Name = "Marital Status")]
        [Required(ErrorMessage = "Mariatal Status is Required.")]
        public int? MariatalStatusId { get; set; }

        [Display(Name = "Marital Status")]
        public string MariatalStatus { get; set; }

        [Display(Name = "Date Of Birth")]
        [Required(ErrorMessage = "Date Of Birth is Required.")]
        [DataType(DataType.Date)]      
        public DateTime? DateOfBirth { get; set; }
        
        [Display(Name = "Cell")]
        [Required(ErrorMessage = "Cell is Required.")]
        public string Cell { get; set; }


    }

Thanks
Kumar

No answers yet. Maybe you can help?

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