如何通过设置不同的 id 来重用局部视图

发布于 2024-08-27 14:39:30 字数 678 浏览 5 评论 0原文

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

 <%=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,但我确信有更好的方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女情怀诗 2024-09-03 14:39:30

听起来您正在使用相同的模型来完成两个部分视图。我看到有两个设置选项。

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

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" })%> 
终止放荡 2024-09-03 14:39:30

您可以只包含要在模型中使用的 id。

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

给我一枪 2024-09-03 14:39:30

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

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

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

In my case, I wanted different IDs because jQuery calendar was failing to write date to the required input field. Instead, it was writing date into the first field with the same ID.

So, I wasn't really concerned with the ID. So used something like this-

@Html.TextBoxFor(model => model.DueDate, new Dictionary<string, Object> { { "class", "datepicker" }, { "id", Guid.NewGuid().ToString() } })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文