2010-10-25 66 views
0

我有一個場景,我必須根據選擇的 下拉列表執行某些操作。通過mvc中的控制器啓用html下拉列表

對於基本參考你可以使用國家,州,城市的例子。

我能夠在當時填寫國家列表,我已將其他兩個下拉列表設置爲 禁用。

國家填充後我想選擇state.it正在填充。

兩個問題

1)如何保持國家的狀態DDL,因爲它是回來了orisnal狀態。 2)如何通過我的控制器啓用下拉菜單。

MyView的代碼

國家

 <p> 
     <label> 
      State</label> 
     <%=Html.DropDownList("ddlState", ViewData["ddlState"] as SelectList, "--not selected--",new { onchange = "document.forms[0].submit()", disabled = "disabled" })%> 
    </p> 

    <p> 
     <label> 
      City</label> 
     <%=Html.DropDownList("ddlCities", ViewData["ddlCities"] as SelectList, "--not selected--", new { onchange = "document.forms[0].submit()", disabled = "disabled" })%> 
    </p> 

我控制器代碼

public ActionResult InsertData() 
    { 

     var customers = from c in objDetailEntity.Country 
         select new 
         { 
          c.Cid, 
          c.Cname 
         }; 

     SelectList countriesList = new SelectList(customers.Take(100), "Cid", "Cname"); 
     ViewData["ddlCountries"] = countriesList; 

     SelectList EmptyState = new SelectList(customers); 
     ViewData["ddlState"] = EmptyState; 
     ViewData["ddlCities"] = EmptyState; 
     Session["ddlSesCountry"] = countriesList; 
     return View(); 

    } 




    // 
    // POST: /RegisTest/Create 

    [HttpPost] 
    public ActionResult InsertData(FormCollection collection) 
    { 
     try 
     { 
      CountryId = Convert.ToInt16(Request.Form["ddlCountries"]); 
      stateid = Convert.ToInt16(Request.Form["ddlState"]); 
      if (CountryId > 0 && stateid <= 0) 
      { 
       var stateslist = from c in objDetailEntity.State 
           where c.Country.Cid == CountryId 
           select new 
           { 
            c.Sid, 
            c.Sname 
           }; 

       SelectList stateList = new SelectList(stateslist.Take(100), "Sid", "Sname"); 
       ViewData["ddlState"] = stateList; 
       Session["StateList"] = stateList; 
       ViewData["ddlCities"] = stateList; 
      } 

      if (stateid > 0) 
      { 
       var citieslist = from c in objDetailEntity.City 
           where c.State.Sid == stateid 
           select new 
           { 
            c.Ctid, 
            c.Cityname 
           }; 

       SelectList cityList = new SelectList(citieslist.Take(100), "Ctid", "Cityname"); 


       ViewData["ddlCities"] = cityList; 
       ViewData["ddlState"] = Session["StateList"]; 
      } 

      ViewData["ddlCountries"] = Session["ddlSesCountry"]; 



      return View(); 
     } 
     catch 
     { 
      return View(); 
     } 
    } 

回答

0

我的選擇是不回發的形式在所有。我將在控制器中編寫一個採用CountryID並返回包含狀態列表的JsonResult的動作。 onchange事件可以調用一個jQuery函數,該函數使用AJAX調用此操作,加載新列表並啓用第二個下拉列表。

但是,如果你堅持回傳,這裏就是爲什麼它不工作:

  1. 的國家列表不保持其選擇的價值,因爲該視圖正每次都從頭開始重新加載和你將其設置爲「未選中」。 SelectList構造函數有一個重載,它將「SelectedItem」對象作爲第四個參數。當你初始化你的SelectList時,你應該傳遞適當的值給這個參數,而不是強制它在View中。

  2. 您需要在視圖中編寫一個「if」子句,以根據某些條件選擇是否啓用列表。例如,您可以綁定到具有布爾屬性(如「EnableStates」)的ViewModel,或者您可以使用類似StateList中值的數量的內容 - 例如,如果計數大於零,則啓用它。

一件棘手的事情習慣,當你從Web窗體移動到MVC是,你不必ViewState中了 - 你的應用程序是無狀態的。沒有什麼能夠「記住」下拉列表中爲您選擇的值,您必須在每次加載頁面時進行設置。

+0

你可以提供一些代碼爲1和2條件 – Ankur 2010-10-26 09:47:19