日期:2014-05-17  浏览次数:20428 次

MVC新手请教:从XML文件获取数据绑定至DROPDOWNLIST。
如题:列出代码如下:
A.XML文件:

<YearTable>
  <yearNumber>2005</yearNumber>
  <yearNumber>2006</yearNumber>
</YearTable>

BMODEL.CS文件属性设置:

       public SelectList LoadYearDataFromXml
        {
            get
            {
                try
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/xmlData_Years.xml"));
                    DataTable dt = ds.Tables[0];
                    EnumerableRowCollection<DataRow> rows = dt.AsEnumerable();
                    List<DataRow> _item = rows.ToList<DataRow>();
                    return new SelectList(_item);
                }
                catch
                {
                    return null;
                }
            }
        }


CCONTROLLER.CS文件:

        // GET: /System_MonthMaxMoneySetting/Create

        public ActionResult Create()
        {
            System_MonthMaxMoneySetting mmms = new System_MonthMaxMoneySetting();
            ViewBag.Items = mmms.LoadYearDataFromXml;
            return View();
        }

最后,D.CSHTML文件:

        <div class="editor-field">
            @Html.DropDownListFor(model => model.IYear,ViewBag.Items as SelectList)
            @Html.ValidationMessageFor(model => model.IYear)
       &n