4

部分的なビューとdisplay/editortemplatesおよびKendouiを利用するasp.netmvc4アプリケーションを構築しようとしています。特定のページのビューモデルがあります。

public class Plant {
    public Guid PlantId{ get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<Leaf> Leafs{ get; set; }
    public ICollection<Flower> Flowers{ get; set; }
    public ICollection<Bug> Bugs{ get; set; }
}

また、葉、虫としての花には独自の特性があります。例:

public class Leaf {
    public Guid LeafId{ get; set; }
    public string Name { get; set; }
    public string Documentation { get; set; }
    public string Description { get; set; }
}

ビューで部分ビューを使用しているので、ajaxでの更新がより簡単になります。私の通常のビュー:PlantDetail.cshtml

@model Plant

<table>
<tr>
    <td>
        <h2>@Html.Label(Resources.PlantDetailTitle)</h2>
    </td>
    <td>
        @Html.HiddenFor(m => m.PlantId)
        @Html.DisplayFor(m => m.Name)
    </td>
</tr>
<tr>
    <td>@Html.LabelFor(m => m.Description)
    </td>
    <td>
        @Html.DisplayFor(m => m.Description)
    </td>
</tr>
</table>

 @{Html.RenderPartial("_flowers", Model);}

 @{Html.RenderPartial("_leafs", Model);}

私の部分ビュー「_leafs」(および「_flowers」)には、LeafIdとPlantIdを必要とするアクションを呼び出す一連のボタンがあります。

部分ビュー"_leafs":

 @model List<Leaf>

 @for (int i = 0; i < Model.Count(); i++ )
  {
  @(Html.DisplayFor(m => m[i]))
 }

私のdisplayTemplate"Leaf.cshtml":

  @model Leaf
  @Html.HiddenFor(m =>m.LeafId)
   <a class='k-button k-button-icontext' href=@Url.Action("InitiateLeaf", "Plant") +"?    
  leafId=@Model.LeafId&plantId=#=PlantId#">@Model.Name</a>

今、私の問題は、displaytemplateで親ビューモデルのPlantIdにアクセスできないように見えることです。(そして、各displaytemplatesで同じ問題が発生します。)url.actionのroutevaluesですでに試しましたが、最終的にjavascriptでPlantIdにアクセスできることはわかっていますが、(mvc)方法はありますか? displaytemplatesを使い続け、私のplantIdを子のLeafビューモデルのプロパティとして複製しませんか?

私はすでに、displaytemplateで「@ HttpContext.Current.Request.RequestContext.RouteData.Values ["controller"]。ToString()」のようなものでparentviewcontextにアクセスしようとしましたが、私の値が見つからないようですPlantId(そこに保存されている場合でも..)。

他の誰かがまだいくつかの提案がありますか?

4

1 に答える 1

1

前述のオプションの1つは、jqueryを使用して親PlantIdにアクセスすることです。親のプロパティにアクセスする必要がある場合は、Entity Frameworkがモデルクラスの作成を推奨する方法と同様に、ビューモデルを設計することをお勧めします。

public class Plant {
    public Guid PlantId{ get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<Leaf> Leafs{ get; set; }
    public ICollection<Flower> Flowers{ get; set; }
    public ICollection<Bug> Bugs{ get; set; }
}

したがって、Leafクラスには、Plantに戻るためのナビゲーションプロパティも必要です。

public class Leaf {
    public Guid LeafId{ get; set; }
    public string Name { get; set; }
    public string Documentation { get; set; }
    public string Description { get; set; }
    public virtual Plant Plant { get; set; }
    public Guid PlantId { get; set; }
}

ViewModelを作成するときは、PlantプロパティとPlantIdを必ず入力してください。お役に立てれば

于 2013-09-11T15:45:04.097 に答える