如何在MVC4 asp.net ajax和entityframework中实现Cascading下拉列表? [英] How to implement Cascading dropdownlist in MVC4 asp.net ajax and entityframework?

查看:78
本文介绍了如何在MVC4 asp.net ajax和entityframework中实现Cascading下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个注册表单,我遇到的问题是哪个类应该绑定我的索引视图,我有三个类Districts,Tehsil,Subtehsil,它们基本上以相同的顺序实现级联下拉列表。

  public   partial  
{
public 区()
{
this .tbTehsils = new HashSet< Tehsil>();
}

public int DistrictId {获得; set ; }
public string DistrictName { get ; set ; }

public virtual ICollection< Tehsil> tbTehsils { get ; set ; }





  public   partial   class  SubTehsil 
{
public int SubTehsilId { get ; set ; }
public string SubTehsilName { get ; set ; }
public Nullable< int> TehsilId { get ; set ; }

public virtual Tehsil tbTehsil {获得; set ; }
}



  public   partial   class  Tehsil 
{
public Tehsil ()
{
this .tbSubTehsils = new HashSet< SubTehsil>() ;
}

public int TehsilId {获得; set ; }
public string TehsilName { get ; set ; }
public int DistrictId { get ; set ; }

public virtual 区域tbDistrict {获得; set ; }
public virtual ICollection< SubTehsil> tbSubTehsils { get ; set ; }





这是我在注册表中的字段



 public partial class ApplicationFormFarmersSolarPower 
{
public int Id {get;组; }
public Nullable < long > UniqueId {get;组; }
public string District {get;组; }
public string Tehsil {get;组; }
public string SubTehsil {get;组; }
public string Village {get;组; }
公共字符串NameOfApplicant {get;组; }
公共字符串NameOfLandOwners {get;组; }
公共字符串ResidentAddress {get;组; }
public Nullable < long > PinCode {get;组; }
public Nullable < long > MobileNumber {get;组; }
public string EmailId {get;组; }
public string NearestStation {get;组; }
公共字符串DistanceOfSubstation {get;组; }
公共字符串AreaOfLand {get;组; }
公共字符串ProposedPlantCapacity {get;组; }
}





i还有一个由EF自动生成的DBContext类,基本上是从数据库中检索数据。 />
现在,当我为DBContext创建强类型视图时,它会出错。



Quote:

传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [FarmerRegisterFormAndPrint.Models.District]',但此字典需要类型为'FarmerRegisterFormAndPrint.Models的模型项。 FarmerDBContext'。





基本上我不明白哪个班级我应该约束我的观点



我在索引中使用了这个代码ActionResult

Quote:



FarmerDBContext db = new FarmerDBContext();

db.Districts.ToList();









i阅读这篇文章,但我失败了了解ProductCatalogclass有什么。



MVC级联Ajax的简单实现在同一网站上下载



我的主要动机是以我的注册表格从数据库生成级联DDL。

解决方案

你好,



也许在你做你的页面渲染时,你没有使用List类..



我发现这篇文章,我认为它可以帮助。< br $> b $ b

http://stackoverflow.com/questions/27378288/but-this-dictionary-requires-a-model-item-of-type-system-collections-generic-ie [ ^ ]





Best Reg ARDS!

I am building a registeration form, i am having problem in which class should i bind my index view to i am having three classes Districts, Tehsil, Subtehsil, which basically implement the cascading dropdownlist in the same order.

public partial class District
    {
        public District()
        {
            this.tbTehsils = new HashSet<Tehsil>();
        }

        public int DistrictId { get; set; }
        public string DistrictName { get; set; }

        public virtual ICollection<Tehsil> tbTehsils { get; set; }



public partial class SubTehsil
    {
        public int SubTehsilId { get; set; }
        public string SubTehsilName { get; set; }
        public Nullable<int> TehsilId { get; set; }

        public virtual Tehsil tbTehsil { get; set; }
    }


public partial class Tehsil
   {
       public Tehsil()
       {
           this.tbSubTehsils = new HashSet<SubTehsil>();
       }

       public int TehsilId { get; set; }
       public string TehsilName { get; set; }
       public int DistrictId { get; set; }

       public virtual District tbDistrict { get; set; }
       public virtual ICollection<SubTehsil> tbSubTehsils { get; set; }



here is my fields in registeration form

public partial class ApplicationFormFarmersSolarPower
   {
       public int Id { get; set; }
       public Nullable<long> UniqueId { get; set; }
       public string District { get; set; }
       public string Tehsil { get; set; }
       public string SubTehsil { get; set; }
       public string Village { get; set; }
       public string NameOfApplicant { get; set; }
       public string NameOfLandOwners { get; set; }
       public string ResidentAddress { get; set; }
       public Nullable<long> PinCode { get; set; }
       public Nullable<long> MobileNumber { get; set; }
       public string EmailId { get; set; }
       public string NearestStation { get; set; }
       public string DistanceOfSubstation { get; set; }
       public string AreaOfLand { get; set; }
       public string ProposedPlantCapacity { get; set; }
   }



i also have a DBContext class genetrated automatically by EF which is basically to retrieve data from the database.
Now when i create strongly typed view against DBContext it gives an error

Quote:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[FarmerRegisterFormAndPrint.Models.District]', but this dictionary requires a model item of type 'FarmerRegisterFormAndPrint.Models.FarmerDBContext'.



basically i am failing to understand which class i should bind my view

I have used this code in index ActionResult

Quote:


FarmerDBContext db = new FarmerDBContext();
db.Districts.ToList();





i read this article but i am failing to understand what the ProductCatalogclass have.

Simple Implementation of MVC Cascading Ajax Drop Down on this same website

My main motive is to generate cascading DDL from database in my registeration form.

解决方案

Hello,

Maybe at the moment of doing the render of your page you are not using the List class..

I found this article and I think it can helps.

http://stackoverflow.com/questions/27378288/but-this-dictionary-requires-a-model-item-of-type-system-collections-generic-ie[^]


Best Regards!


这篇关于如何在MVC4 asp.net ajax和entityframework中实现Cascading下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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