2011-06-18 56 views
1

XML文件內容我有這樣的XML文件顯示在Html.DropDownList

<?xml version="1.0" encoding="utf-8"?> 
<CRM> 
    <Unit ID="1" Name="آذربایجان شرقی"> 
    <city ID="1">آذرشهر </city> 
    <city ID="2">اسکو </city> 
    <city ID="3">اهر </city> 
    <city ID="12">کلیبر </city> 
    <city ID="13">مراغه </city> 
    <city ID="14">مرند </city> 
    <city ID="15">ملکان </city> 
    <city ID="16">ملکان </city> 
    <city ID="17">میانه </city> 
    <city ID="18">ورزقان </city> 
    <city ID="19">هریس </city> 
    <city ID="20">هشترود</city> 
    </Unit> 

    <Unit ID="2" Name="آذربایجان غربی"> 
    <city ID="1">ارومیه </city> 
    <city ID="2">اشنویه </city> 
    <city ID="3">بوکان </city> 
    <city ID="4">پیرانشهر </city> 
    <city ID="5">تکاب </city> 
    <city ID="6">چالدران </city> 
    </Unit> 

    <Unit ID="3" Name="اردبیل"> 
    <city ID="1">اردبیل </city> 
    <city ID="2">بیله‌سوار </city> 
    </Unit> 

    <Unit ID="4" Name="اصفهان"> 
    <city ID="1">آران و بیدگل</city> 
    <city ID="2">اردستان </city> 
    <city ID="3">اصفهان </city> 
    <city ID="4">برخوار و میمه</city> 
    <city ID="5">تیران و کرون</city> 
    <city ID="6">چادگان </city> 
    <city ID="7">خمینی‌شهر </city> 
    <city ID="8">خوانسار </city> 
    <city ID="9">سمیرم </city> 
    <city ID="10">شهرضا"</city> 
    <city ID="11">سمیرم سفلی"</city> 
    <city ID="12">فریدن"</city> 
    </Unit> 
</CRM> 

我展示列表名稱單位html.dropdownlist

我用這個代碼:

List<SelectList> u = new List<SelectList>(); 

var locations = XDocument.Load(strXmlpath); 
var query = from l in locations.Descendants("CRM") select l; 

foreach (var q in query) 
{ 
    u.Add(new SelectList(new[] { 
           new {ID = q.Attributes("ID").FirstOrDefault(), 
            Name = q.Attributes("Name").FirstOrDefault() 
            } 
          }, 
         "ID", "Name", 1)); 
} 

ViewData["xml"] = new SelectList(u,"ID","Name"); 

這是在該圖中:

<%=Html.DropDownList("xml",(SelectList) ViewData["xml"])%> 

所但是,導致此錯誤:

DataBinding: 'System.Web.Mvc.SelectList' does not contain a property with the name 'ID'.

回答

3

我會用一個視圖模型:

public class UnitViewModel 
{ 
    public string SelectedID { get; set; } 
    public IEnumerable<SelectListItem> Units { get; set; } 
} 

我會解析XML文件後,填充在我的控制器動作:

public ActionResult Index() 
{ 
    // TODO: it would be better to externalize the parsing of the XML 
    // file into a separate repository class to avoid cluttering your 
    // controller actions with such code which is not what they should 
    // be responsible for. But for the purpose of this answer it should 
    // be enough 

    var file = Path.Combine(Server.MapPath("~/app_data"), "crm.xml"); 
    var model = new UnitViewModel 
    { 
     Units = 
      from unit in XDocument.Load(file).Document.Descendants("Unit") 
      select new SelectListItem 
      { 
       Value = unit.Attribute("ID").Value, 
       Text = unit.Attribute("Name").Value 
      } 
    }; 
    return View(model); 
} 

和最後在我的強類型視圖中,我將生成基於此視圖模型的下拉列表:

<%= Html.DropDownListFor(
    x => x.SelectedID, 
    new SelectList(Model.Units, "Value", "Text") 
) %> 
+0

坦。但如果想從ViewDate中使用,應該怎麼做?例如ViewData [「xml」] = model; – user818566

+0

@ user764499,我個人討厭'ViewData'。我從來沒有使用它,我永遠不會推薦給任何人。我總是喜歡使用強類型的視圖模型。 –

+0

但我的頁面包括許多部分,例如列表單元從xml文件顯示在dropdoewnlist和列表活動從其他xml文件顯示在下拉列表和文本框中輸入詳細信息客戶我該怎麼做? – user818566

0

所以我使用這個主題和其他一些來找出我需要的修復我的國家dropdownlist。我想它可能會幫助人們在未來。

這就像一個魅力可能不是乾淨的解決方案,但嘿它的工作原理

 var file = Path.Combine(Server.MapPath("~/Content/xml"), "countries.xml"); 
     XmlDocument doc = new XmlDocument(); 

     doc.Load(file); 
     XmlNodeList countryList = doc.GetElementsByTagName("Country"); 

     List<SelectListItem> countries = new List<SelectListItem>(); 

     foreach (XmlNode country in countryList) 
     { 
      string name = country.ChildNodes[0].InnerText; 
      string code = country.ChildNodes[1].InnerText; 


      countries.Add(new SelectListItem 
      { 
       Text = name, 
       Value = code 
      }); 
     } 

     ViewBag.country = new SelectList(countries, "Value", "Text"); 

     return View();