0

ビューにいくつかのプロパティを渡そうとしていますが、間違ったプロパティが表示されます。モデルは次のとおりです。

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 i が選択した Id です。やってるからかな

return View(module.FirstOrDefault());

正しいアイテムとそのプロパティを View に渡すにはどうすればよいですか?

4

3 に答える 3

1

Id = 何らかの ID を持つ適切なアイテムを表示する場合は、ID でレコードを選択する必要があります。コードで、linq select ステートメントに where 句を追加します。

  var module = (SbSis.Modules.Where(t => t.ID == id).Select( ....
于 2013-07-02T09:13:51.793 に答える
0

に従って要素を返したい場合はid、これでフィルタリングできます。module.FirstOrDefault(x=> x.Id == id)

于 2013-07-02T09:17:47.047 に答える