我正在尝试将一些属性传递给查看,但我得到了错误的视图。这是模型:
public class ModuleDetails {
public long Id { get; set; }
public string ModuleId { get; set; }
public string TypeName { get; set; }
public string KindName { get; set; }
public IEnumerable<Property> Properties { get; set; }
}
public class Property {
public string Name { get; set; }
public string Value { get; set; }
}
这是我在控制器中所做的:
public ActionResult Details(long id) {
var ownerId = _dbSis.OwnedModules.Find(id);
var ownerName = _dbSis.Set<BusinessUnit>().Find(ownerId.ModuleOwnerId);
var module = (_dbSis.Modules.Select(m => new ModuleDetails {
Id = id,
ModuleId = m.ModuleId,
TypeName = m.ModuleType.TypeName,
KindName = m.ModuleType.ModuleKind.KindName,
Properties = m.PropertyConfiguration.PropertyInstances.Select(
x => new Property {Name = x.Property.Name, Value = x.Value})
}));
return View(module.FirstOrDefault());//am i doing something wrong here?
}
和视图:
@using BootstrapSupport
@model AdminPortal.Areas.Hardware.Models.ModuleDetails
@{
ViewBag.Title = "Details";
Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}
<fieldset>
<legend>Module <small>Details</small></legend>
<dl class="dl-horizontal"> <!-- use this class on the dl if you want horizontal styling http://twitter.github.com/bootstrap/base-css.html#typography class="dl-horizontal"-->
<dt>ID</dt>
<dd>@Model.Id</dd>
<dt>Module ID</dt>
<dd>@Model.ModuleId</dd>
<dt>Module Type</dt>
<dd>@Model.TypeName</dd>
<dt>Module Kind</dt>
<dd>@Model.KindName</dd>
@foreach (var properties in Model.Properties)
{
<dt>Property Names</dt>
<dd>@properties.Name</dd>
<dt>Property Value</dt>
<dd>@properties.Value\</dd>
}
</dl>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", Model.GetIdValue()) |
@Html.ActionLink("Back to List", "ModuleList")
</p>
现在,当我运行程序并在控制器的操作中设置断点时,我得到了这个
我可以看到属性中有一些名称和值。但在视图中,无论我选择哪个项目,我总是得到第一个项目的详细信息,但 ID 是 Id 是我选择的 Id。是不是因为我在做
return View(module.FirstOrDefault());
如何将正确的项目及其属性传递给 View?