如何在通用存储库模式Mvc.Net实体框架中实现级联下拉列表? [英] How to achieve cascading dropdown list in generic repository pattern Mvc.Net entity framework?

查看:238
本文介绍了如何在通用存储库模式Mvc.Net实体框架中实现级联下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何首先使用EF Code在通用存储库模式中创建级联的DropDownList,其中在该可用国家/地区和城市选项中有一个注册表格,该表格来自不同的数据库表。

公共类ManupulationRecord:IHallBook其中T:我通过 HallProfile实体模型的类
现在是
如何实现城市国家州级联下拉列表在视图中添加新记录

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; }
}

在接口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();
 }

操纵Calss在这里

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);
        }

控制器

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();
        }
    }


推荐答案

尝试添加到控制器: private BAL.interfaces.IHallBook< ClsCityList> cityList;
并在运行中AddNewHallRecord:

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;

并认为:

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

这篇关于如何在通用存储库模式Mvc.Net实体框架中实现级联下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆