[英]Displaying an XML file on a view in ASP.Net MVC5
I am relatively new when it comes to asp.net MVC 5 and I am trying to display the contents of an xml file ( which is located in the content folder of my project for ease of access) and display the contents on a simple view. 对于asp.net MVC 5,我是一个相对较新的人,我试图显示xml文件的内容(该文件位于我项目的内容文件夹中,以便于访问),并在简单视图中显示内容。 the code from my controller, modelclass and view are below.....
来自我的控制器,模型类和视图的代码在下面.....
The Error I am getting is as follows 我得到的错误如下
The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[System.Xml.Linq.XElement]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[XML_v1._0.Models.XML_details]'.传递到字典中的模型项的类型为'System.Collections.Generic.List
1[System.Xml.Linq.XElement]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [XML_v11[System.Xml.Linq.XElement]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
._0.Models.XML_details]”。
The code for my controller, modelclass and view are below..... 我的控制器,模型类和视图的代码如下.....
@model IEnumerable<XML_v1._0.Models.XML_details> } <head> <title></title> <style> th { font-size: 300%; } td { font-size: 300%; } </style> </head> <h2>Line Status</h2> <div> <table> <tr> <th> @Html.DisplayNameFor(model => model.Line) </th> <th> @Html.DisplayNameFor(model => model.Status) </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Line) </td> <td> @Html.DisplayFor(modelItem => item.Status) </td> </tr> } </table> </div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XML_v1._0.Models;
using System.Xml.Linq;
using System.Xml;
using System.Text;
using System.Collections;
using System.Dynamic;
namespace XML_v1._0.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
XDocument L_tube = XDocument.Load(@"C:\Users\070339\Documents\Visual Studio 2013\Projects\Learning and practice\XML v1.0\XML v1.0\Content\tube.xml");
var train = from s in L_tube.Descendants() select s;
var model = train.ToList();
return View(model);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
namespace XML_v1Models
{
public partial class XML_details
{
public XmlElement Line { get; set; }
public XmlElement Status { get; set; }
}
}
Any help would be appreciated. 任何帮助,将不胜感激。
When you do a return view()
in your controller you should pass it a variable of type IEnumerable<XML_v1._0.Models.XML_details>
. 在控制器中执行
return view()
,应向其传递IEnumerable<XML_v1._0.Models.XML_details>
类型的变量。 So basically your controller would do the following 因此,基本上,您的控制器将执行以下操作
IEnumerable<XML_v1._0.Models.XML_details> model = (from s in
L_tube.Descendants() select s);
and then do the 然后做
return View(model);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.