我很确定我的语法有误。这是foreach语句的详细视图。在索引视图中,我正在显示每条记录。我只想要一个详细信息视图,当您单击该视图时将显示一条记录。我尝试将模型类更改为iEnum,iList和其他一些东西。有没有一种方法可以使视图从XML文件中打印出一条记录?在下面的项目。谢谢。
查看:(详细信息)
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Personal</h4>
<hr />
@foreach (PersonsApplicationFromXMLFile.Models.Personal persons in Model)
{
<dl class="dl-horizontal">
<dt>
Name
</dt>
<dd>
@persons.Name
</dd>
<dt>
Gender
</dt>
<dd>
@persons.Gender
</dd>
<dt>
City
</dt>
<dd>
@persons.City
</dd>
</dl>
}
</div>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
查看:(索引)
@{
ViewBag.Title = "Index";
}
<style>
table tr td {
padding: 0 20px;
border:5px solid black;
}
</style>
@Html.ActionLink("Create new Record", "Create")
<table>
<tr>
<th>@Html.ActionLink("ID", "Index", new { sortOrder = "ID" })</th>
<th>@Html.ActionLink("Name", "Index", new { sortOrder = "Name" })</th>
<th>Gender</th>
<th>@Html.ActionLink("City", "Index", new { sortOrder = "City" })</th>
</tr>
@foreach (PersonsApplicationFromXMLFile.Models.Personal personal in Model)
{
<tr>
<td>@personal.ID</td>
<td>@personal.Name</td>
<td>@personal.Gender</td>
<td>@personal.City</td>
<td>@Html.ActionLink("Details", "Details") |</td>
</tr>
}
</table>``
控制器:(详细信息)
// GET: Person/Details/5
public ActionResult Details(string ID)
{
XmlDocument doc = new XmlDocument();
doc.Load(Some file...);
IEnumerable<Personal> persons = doc.SelectNodes("/Persons/record")
.Cast<XmlNode>()
.Select(node => new Personal()
{
ID = node["ID"].InnerText,
Name = node["Name"].InnerText,
Email = node["Email"].InnerText,
DateOfBirth = node["DateOfBirth"].InnerText,
Gender = node["Gender"].InnerText,
City = node["City"].InnerText
});
return View(persons.ToList());
}
答案 0 :(得分:3)
我猜您想在详细信息中打印具有指定ID的数据。 解决方法是这里。
for (let i = 0, len = document.body.children.length; i < len; i++) {
document.body.children[i].onclick = function(){
alert(i) ;
}
}
在视图中
public ActionResult Details(string ID)
{
XmlDocument doc = new XmlDocument();
doc.Load(Some file...);
Personal person = doc.SelectNodes("/Persons/record")
.Cast<XmlNode>()
.Where(node=>ID.Equals(node["ID"].InnerText)
.Select(node => new Personal()
{
ID = node["ID"].InnerText,
Name = node["Name"].InnerText,
Email = node["Email"].InnerText,
DateOfBirth = node["DateOfBirth"].InnerText,
Gender = node["Gender"].InnerText,
City = node["City"].InnerText
}).FirstOrDefault();
return View(person);
}