0

XML コンテンツを MVC4 のドロップダウン リストにバインドしています。リンクから、XMLファイルの内容を読み込んでみました。コードの Where 句は、オブジェクトのインスタンスを参照していないことを返します。where 条件がない場合、xml コンテンツはファイルから読み取られます。大文字の ID=1 に基づく値のみを表示したい。
XML

<?xml version="1.0" encoding="utf-8"?>
<State>
<Capital ID="1" CountryName="India">
    <city name="Andaman and Nicobar Islands" value="AN"></city>
    <city name="Andhra Pradesh" value="AP"></city>
</Capital>  
<Capital ID="2" CountryName="USA">
    <city name="Alabama" value="AL"></city>
    <city name="Alaska" value="AK"></city>
</Capital>  
</State>

コントローラ

 public ActionResult Details()
{
 string UserID = 12345;
 string partnerid = XCWERT;
 var file = Path.Combine(Server.MapPath("~/App_Data"), "States.xml");            
 var model = new CheckoutModel
 {    
  States =
  from unit in XDocument.Load(file).Document.Elements("State").Elements("Capital").Elements("city") -- This loads all the values
  //   from unit in XDocument.Load(file).Document.Descendants("Capital").Where(unit => (string)unit.Attribute("ID").Value == "1") -- where condition is not accepting and throwing error
   select new SelectListItem
  {
    Text = unit.Attribute("name").Value,
    Value = unit.Attribute("value").Value,                      
  }                     
};
 SelectList selectList = new SelectList(model.States, "Value", "Text");
 ViewData["StateOptions"] = selectList;          
 return View(GetShippingAddress(UserID, partnerid));
 }

意見

 @Html.DropDownList("State", ViewData["StateOptions"] as IEnumerable<SelectListItem>, "(Select one)", new { @class = "TextBoxBorder" })

私のコードの間違いは何ですか。? 提案は非常に役立ちます

4

1 に答える 1