【问题标题】:How to achieve cascading dropdown list in generic repository pattern Mvc.Net entity framework?如何在通用存储库模式 Mvc.Net 实体框架中实现级联下拉列表?
【发布时间】:2019-09-18 05:51:13
【问题描述】:

如何首先使用 EF 代码在通用存储库模式中创建级联 DropDownList,其中可用国家/地区州和城市选项中的一个注册表单从不同的数据库表中获取
公共类 ManupulationRecord : IHallBook where T : 类 我通过“HallProfile”实体模型的地方 现在如何实现City Country State级联下拉列表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; }
}

在接口调用中

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

手动调用就在这里

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

【问题讨论】:

    标签: c# sql-server asp.net-mvc entity-framework ef-code-first


    【解决方案1】:

    尝试添加到控制器:private BAL.interfaces.IHallBook&lt;ClsCityList&gt; cityList; 并在行动 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" })
    

    【讨论】:

    • 先生它可以工作但需要相同的控制器先生只需要使用通用存储库显示注册表如果您想更改任何代码或添加新代码只是想要输出它应该可用的城市国家国家下拉列表
    • 请先生帮帮我
    • 我不明白你的意思,在行动 AddNewHallRecord(ClsCityList city) 你想要通过 ClsCityList city 的城市列表、州列表、国家列表?你能给我你的界面图片吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多