views:

160

answers:

2

i have a partial view with a dropdown in it. the code looks like this:

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

the issue is that i want to reuse this partial view in multiple places but i want to have a different id assigned to the control (instead of exerciseDropdown always) but on the outside i only have this . .

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

is there anyway to pass in an id into a partial view. is there a standard way to inject the ids into a partial view ?

right now i am doing things like this:

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

where ExerciseList and ExerciseList2 are identical but with different ids but i am sure there is a better way.

A: 

You could just include the id you want to use in the model.

NickLarsen
@NickLarsen - can you clarify with an example here?
ooo
+2  A: 

It sounds like you are using the same model to do both partial views. I see two options for setting this up.

  1. Like Nick said above, use a partial class and add the dropDownID property to your model.

**Warning Pseudo VB Code ahead

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

Then in your controller:

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

View:

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

Option 2: Set in your ViewData dictionary

Controller:

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

Call Partial View:

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

View:

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