Fill Drop down List Using VIew Model in MVC





=================== view model start =====================
 public class TourMaster_SubCategoryViewModel
    {
        public int SubCategoryId { get; set; }
        [Display(Name = "Category")]
        [Required(ErrorMessage = "Please select category")]
        public int CategoryId { get; set; };
        public IEnumerable<TourMaster_CategoryViewModel> Categories { get; set; }
        [Display(Name = "Sub category name")]
        public string Name { get; set; }
        [Display(Name = "Category name")]
   }
=================== view model end =====================



=================== CONTROLLER START (page load) =====================
 public ActionResult subcategorydetails(int? vbgid)
        {
            Models.TourMaster_SubCategoryViewModel _adminViewModel = objMasterRepository.GetSubCategoryDetail(vbgid);
            _adminViewModel.Categories = objMasterRepository.GetCategoryList().Where(x => x.IsActive == true);
            return View(_adminViewModel);
        }
=================== CONTROLLER END (page load) =====================



=================== cshtml start =====================

 @Html.DropDownListFor(m => m.CategoryId, new SelectList(Model.Categories,"CategoryId","CategoryName"), "--Please select category--", new { @type = "text", @class = "form-control"})

                                                        @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "error" })

================= cshtml end ====================

Comments

Popular posts from this blog

Tree view in winforms using c#

how to Replace null value with 0 Using C#

how to fetch all HTML Table Records and modify all records accordingly using jquery and Javascript