2012-03-06 125 views
0

我創造MVC3搜索應用程序,我有2個表:ID(PK),S_ID(FK),District_name如何使用MVC3在下拉列表中顯示數據?

我: 1.State:ID(PK)和STATE_NAME 2.District首先使用代碼和EF和具有數據庫創建了它所謂的搜索

我希望我的索引顯示在下拉所有狀態下拉列表

以下是我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> 

With這我能夠看到我的第一ddl,但我怎麼能綁定它與我的第二個名單.....

我需要的代碼,幫助我只顯示選定的狀態區.....只要任何人幫助我與jQuery代碼...

謝謝你提前!

+1

http://stackoverflow.com/questions/5096204/how-do-i-use-the-dropdownlist-in-asp-net-mvc-3 http://stackoverflow.com/questions/5098848/help-我明白瞭解如何工作與dropdownlistfor-in-mvc3 – Henry 2012-03-06 07:16:20

+0

@HenryP:thnx幫助我..... bt我們不能使用sum事物而不是視圖模型....我的意思是它需要創建它.... – EqEdi 2012-03-06 08:39:34

+0

@亨利普:鏈接你給工作以及bt不給我解決方案....可以ü幫助我與總和代碼....我已更新我的代碼 – EqEdi 2012-03-07 10:42:55

回答

1

你試圖實現一個動態/級聯下拉列表,並有在互聯網上大量的資源,它可以幫助您與此:

一個簡單的非AJAX實現可以這樣工作:

你有你的第一個下拉列表,通常創建:

@Html.DropDownListFor(x => x.states, new SelectList(Model.states, "Id", "State_Name"), new {id = "states"}) 

你的第二個下拉菜單,或相關的下拉列表中,將需要存儲哪些父項目的每個項目相關聯的一些信息。例如:

<select name="selecteddistrict" id="districts"> 
@foreach (var district in Model.districts) 
{ 
    <option value="@district.Id" data-state-id="@district.StateId">@district.District_name</option> 
} 
</select> 

請注意下拉菜單中的每個條目如何具有附加屬性data-state-id。我們需要這些信息來了解給定當前活動狀態的值。

然後您可以添加相應的事件處理程序負責處理您的主要下拉的變化事件,並更新相關的下拉列表:

<script type="text/javascript"> 

$(function() { 
    $("#states").change(onStateChange).change(); // fire for initial setup 
}); 

function onStateChange() { 
    var districtDropdown = $('#districts'); 
    districtDropdown.find('option').hide(); 
    var districtsForState = $(districtDropdown.find('option[data-state-id=' + $(this).val() + ']')); 
    districtsForState.show(); 
    districtDropdown.val(districtsForState.val()); 
} 

</script> 

請注意所提供的代碼是隻說明概念。

+0

:你是soooo很多... bt的數據狀態ID是不承認....所以我可以使用ID而不是 – EqEdi 2012-03-07 11:56:13

+0

Thanx噸...它工作...救了我的生命:) – EqEdi 2012-03-07 12:01:25

+0

你能告訴我怎麼可以我得到選定的狀態ID並傳遞給控制器​​? – EqEdi 2012-03-08 10:57:53

相關問題