How do you reuse partial view with setting different identifiers

I have a partial view with a drop down list. The code is as follows:

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

The problem is that I want to reuse this partial view in several places, but I want to have a different identifier assigned to the control (instead of always doing the exercise), but outside I have only this:

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

Do I need to pass id to a partial view? Is there a standard way to enter identifiers in partial view?

Now I am doing things like this:

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

where ExerciseList and ExerciseList2 are identical, but with different identifiers, but I'm sure there is a better way.

+3
3

, . .

  • , dropDownID .

** Pseudo VB Code

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​​p >

:

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" })%> 
+3

, .

0

, jQuery . .

, ID. - -

@Html.TextBoxFor(model => model.DueDate, new Dictionary<string, Object> { { "class", "datepicker" }, { "id", Guid.NewGuid().ToString() } })
0

Source: https://habr.com/ru/post/1739754/


All Articles