4

我有一个带有下拉列表的部分视图。代码如下所示:

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

问题是我想在多个地方重用这个局部视图,但我想为控件分配一个不同的 id(而不是总是用 exerciseDropdown),但在外面我只有这个:

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

反正有没有将 id 传递到局部视图中?是否有将 id 注入局部视图的标准方法?

现在我正在做这样的事情:

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

其中 ExerciseList 和 ExerciseList2 相同但具有不同的 ID,但我相信有更好的方法。

4

3 回答 3

4

听起来您使用相同的模型来执行两个部分视图。我看到了两个设置选项。

  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 回答
0

就我而言,我想要不同的 ID,因为 jQuery 日历无法将日期写入所需的输入字段。相反,它将日期写入具有相同 ID 的第一个字段。

所以,我并不真正关心ID。所以用了这样的东西-

@Html.TextBoxFor(model => model.DueDate, new Dictionary<string, Object> { { "class", "datepicker" }, { "id", Guid.NewGuid().ToString() } })
于 2014-04-23T09:33:18.647 回答