How to achieve cascading dropdown list in generic repository pattern Mvc.Net entity framework?

老子叫甜甜 提交于 2019-12-11 17:28:42

问题


How to create a cascading DropDownList in generic repository pattern using EF Code first, where one registration form in that available country state and city option and this get from different database table
public class ManupulationRecord : IHallBook where T : class where I passed "HallProfile" ENtity Model now How achieve City Country State cascading drop down list Adding New Record in View

namespace ModelData
{
   [Table("tblHallProfile")]
  public class HallProfile
    {


        [Key]
        public int profileId { get; set; }
        public string hallName { get; set; } 
        public string thumbnailImgPath { get; set; }
        public string hallOwnerName { get; set; }
        public string adress { get; set; }
        public string contactNumber { get; set; }
        public string emailAdress { get; set; }
        public string website { get; set; }

        public string hallCategory { get; set; }
        public string createdDate { get; set; }

        public int vehecleParking { get; set; }
        public string foodTypeVegNonVej { get; set; }
        public string decription { get; set; } 
        public decimal cost { get; set; }
        public string serviceTime { get; set; }
        public string OtherService { get; set; }
        public int publicCapacity { set; get; }
        public int cityId { get; set; }
        [ForeignKey("cityId")]
        public virtual ClsCityList cityIdt { get; set; }

        public int StateId { get; set; }
        [ForeignKey("StateId")]
        public virtual ClsStateList stateIdt { get; set; }

        public int CountryId { get; set; }
        [ForeignKey("CountryId")]
        public virtual  ClsCountryList CountryIdt { get; set; }

        public int areaId { get; set; }
        [ForeignKey("areaId")]
        public virtual ClsArea areaIdt { get; set; }
}

In Interface Calss

public interface IHallBook<T> where T : class
{
    T GetRecord(int id);
    IEnumerable<T> GetByCity(string city);
    IEnumerable <T>GetAllList();
    void AddNewRecord(T model);
    void UpdateRecord(T model);
    void DeleteRecord(int id);

    void SaveAsRecord();
 }

Manupulation Calss is Here

public class ManupulationRecord<T> : IHallBook<T> where T : class
    {
        private DataBaseContext dbdata;
        private DbSet<T> dbEntity;

        public ManupulationRecord()
        {
            dbdata = new DataBaseContext();
            dbEntity = dbdata.Set<T>();
        } 
        public void AddNewRecord(T model)
        {
            dbEntity.Add(model);
        }

controller

public class AllHallListController : Controller
    {
        // GET: /AllHallList/Index
        private BAL.interfaces.IHallBook<HallProfile> fromintrface;

        public AllHallListController()
        {
            this.fromintrface = new ManupulationRecord<HallProfile>();
        }

        public ActionResult Index()
        {
            ViewBag.listdata = from x in fromintrface.GetAllList() select x;
            return View(from x in fromintrface.GetAllList() select x);
        }
        // AllHallList/AddNewHallRecord

        [HttpGet]
        public ActionResult AddNewHallRecord(ClsCityList city)
        { 
            return View();
        }
        [HttpPost]
        public ActionResult AddNewHallRecord(HallProfile hall)
        {
            fromintrface.AddNewRecord(hall);
            fromintrface.SaveAsRecord();
            return View();
        }
    }

回答1:


Try add to controller: private BAL.interfaces.IHallBook<ClsCityList> cityList; and in action AddNewHallRecord:

ViewBag.CityList = (from x in cityList.GetAllList() select x);
SelectList selectlistCity = new SelectList(city, "Id", "CityName");
ViewBag.CityList = selectlistCity;

and in view:

@Html.DropDownList("CityDropDown", (IEnumerable<SelectListItem>) ViewBag.CityList , new { @class = "form-control" })


来源:https://stackoverflow.com/questions/57985873/how-to-achieve-cascading-dropdown-list-in-generic-repository-pattern-mvc-net-ent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!