我有一个部分视图,其中包含下拉列表。代码如下所示:
<%=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,但我确信有更好的方法。
答案 0 :(得分:3)
听起来您使用相同的模型来执行两个部分视图。我看到两个选项来设置它。
**提前警告伪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" })%>
答案 1 :(得分:0)
您可以只包含要在模型中使用的ID。
答案 2 :(得分:0)
就我而言,我想要不同的ID,因为jQuery日历无法将日期写入所需的输入字段。相反,它是将日期写入具有相同ID的第一个字段。
所以,我并不真正关心身份证。所以使用这样的东西 -
@Html.TextBoxFor(model => model.DueDate, new Dictionary<string, Object> { { "class", "datepicker" }, { "id", Guid.NewGuid().ToString() } })