Asp.net mvc 将属性从控制器传递到视图不工作?还是我通过了错误的财产?
我试图传递一些属性以供查看,但我在视图中得到了一个错误的属性。 这是型号:Asp.net mvc 将属性从控制器传递到视图不工作?还是我通过了错误的财产?,asp.net-mvc,asp.net-mvc-4,view,controller,Asp.net Mvc,Asp.net Mvc 4,View,Controller,我试图传递一些属性以供查看,但我在视图中得到了一个错误的属性。 这是型号: 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<P
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=some Id的适当项,则需要按Id选择记录。在代码中,向linq select语句添加where子句,它可以是
var module = (SbSis.Modules.Where(t => t.ID == id).Select( ....
如果要根据
id
返回元素,可以使用以下内容进行筛选:module.FirstOrDefault(x=>x.id==id)
可能.FirstOrDefault()方法的结果是默认值。这意味着您可以获得多个对象,但视图具有对象模型,而不是IEnumerable。请按以下步骤替换代码以进行测试
从
到
如果在这之后你不会出错,那就意味着我是对的
var module = (SbSis.Modules.Where(t => t.ID == id).Select( ....
return View(module.FirstOrDefault());
return View(module.First());