4

ドロップダウンのある部分ビューがあります。コードは次のようになります。

 <%=Html.DropDownListFor(x => Model.Exercises, new SelectList(Model.Exercises, "Id", "Name", Model.SelectedExercise), new { @id = "exerciseDropdown", @class = "autoComplete" })%>

問題は、この部分的なビューを複数の場所で再利用したいのですが、(exerciseDropdown ではなく) コントロールに別の ID を割り当てたいのですが、外側ではこれしかありません。

 <% Html.RenderPartial("ExerciseList", Model); %>

IDを部分ビューに渡す方法はありますか? ID を部分ビューに挿入する標準的な方法はありますか?

現在、私は次のようなことをしています:

 <% Html.RenderPartial("ExerciseList", Model); %>
 <% Html.RenderPartial("ExerciseList2", Model); %>

ここで、ExerciseList と ExerciseList2 は同じですが、ID が異なりますが、もっと良い方法があると確信しています。

4

3 に答える 3

4

同じモデルを使用して両方の部分ビューを実行しているようです。これを設定するための 2 つのオプションがあります。

  1. 上で Nick が言ったように、部分クラスを使用して、dropDownID プロパティをモデルに追加します。

**疑似 VB コードの先に警告

Partial Public Class Exercise
Dim _ddID as string

Public Property DropDownID as string
    Get
        Return _ddID
    End Get
    Set(byval value as string)
        _ddID = value
    End Set
End Property

次に、コントローラーで:

Function Index() as ActionResult
    Exercise = getExercise() 'call to get your exercise model'
    Exercise.DropDownID = "DropDownID1"
    Return View(Exercise)
End Function

意見:

 <%=Html.DropDownListFor(x => Model.Exercises, new SelectList(Model.Exercises, "Id", "Name", Model.SelectedExercise), new { @id = model.DropDownID, @class = "autoComplete" })%> 

オプション 2: ViewData ディクショナリに設定する

コントローラ:

Function Index() as ActionResult
    Exercise = getExercise() 'call to get your exercise model'
    ViewData("ExerciseID") = "DropDownID1"
    Return View(Exercise)
End Function

通話部分ビュー:

<% Html.RenderPartial("ExerciseList", Model, ViewData); %> 

意見:

 <%=Html.DropDownListFor(x => Model.Exercises, new SelectList(Model.Exercises, "Id", "Name", Model.SelectedExercise), new { @id = ViewData("ExerciseID"), @class = "autoComplete" })%> 
于 2010-04-05T10:57:31.263 に答える
0

モデルで使用したい ID を含めることができます。

于 2010-04-04T13:27:07.253 に答える