c# - 在 Html.DropDownList 中显示 XML 文件内容

标签 c# xml asp.net-mvc

我有这个xml文件

<?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");

这是在 View 中:

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

但是会导致这个错误:

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

最佳答案

我会使用 View 模型:

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

在解析 XML 文件后,我将在我的 Controller 操作中填充它:

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);
}

最后,在我的强类型 View 中,我将基于此 View 模型生成下拉列表:

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

关于c# - 在 Html.DropDownList 中显示 XML 文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6396847/

相关文章:

c# - 为一组随机值生成多个范围

c# - ICollection<T>.Any(Func<T, bool>) 的表达式

java - JAXB 抽象类和 XmlID/XmlIDREF

c# - 完成操作后如何销毁类的实例

c# - ASP.Net MVC Viewmodels 如何将 View 模型中的值映射到数据库表

c# - 返回适当的消息给主叫客户端

python - 阿拉伯文本不仅在 lxml 输出中显示为字符实体

xml - Android ImageButton - 无法在中心获取图像

c# - 将 HttpWebResponse 转换为 HttpResponseMessage

c# - .NET Core 项目与 .NET Framework 中为 System.Data.DataTable 生成的 JSON 的变化