【发布时间】:2012-03-06 07:13:09
【问题描述】:
我正在 mvc3 中创建一个搜索应用程序,其中有 2 个表: 1.State :Id(pk) 和 state_name 2.District:Id(pk),s_id(f.k.), District_name
我首先使用代码和 EF,并为其创建了名为 Search 的数据库
我希望我的索引在下拉列表中显示所有状态
以下是我的 State.cs 代码
public partial class State
{
public State()
{
this.Districts = new HashSet<District>();
this.Search_master = new HashSet<Search_master>();
}
public int Id { get; set; }
public string State_name { get; set; }
public virtual ICollection<District> Districts { get; set; }}
这是我的区课:
public partial class District
{
public District()
{
this.Search_master = new HashSet<Search_master>();
}
public int Id { get; set; }
public string District_name { get; set; }
public int StateId { get; set; }
public virtual State State { get; set; } }
我还为州和地区创建了一个视图模型......
public class State_district
{
public string selectedstate { get; set; }
public IEnumerable<State> states { get; set; }
public string selecteddistrict { get; set; }
public IEnumerable<District> districts { get; set; }
}
我写的控制器内部:
public ActionResult Index()
{
var model = new State_district { states = db.States, districts = db.Districts };
return View(model);}
在视图中:
<div class="editor-field" id="districtID">*Select State:-
@Html.DropDownListFor(x => x.states, new SelectList(Model.states, "Id", "State_Name"))
</div>
有了这个,我可以看到我的第一个 ddl,但是我怎样才能将它与我的第二个列表绑定.....
我需要代码来帮助我只从选定的州向我展示地区.....任何人都可以帮助我使用 jQuery 代码...
提前谢谢你!!
【问题讨论】:
-
@HenryP:thnx 帮助我.....bt 我们不能用 sum 的东西代替视图模型......我的意思是有必要创建它......
-
@HenryP:你提供的链接工作得很好,bt 没有给我解决方案....你能帮我总结代码吗....我已经更新了我的代码
标签: jquery asp.net-mvc asp.net-mvc-3 drop-down-menu