如何通过设置不同的 id 来重用局部视图
我有一个带有下拉菜单的部分视图。代码如下所示:
<%=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您正在使用相同的模型来完成两个部分视图。我看到有两个设置选项。
**警告前面的伪VB代码
然后在你的控制器中:
视图:
选项2:在你的ViewData字典中设置
控制器:
调用部分视图:
视图:
It sounds like you are using the same model to do both partial views. I see two options for setting this up.
**Warning Pseudo VB Code ahead
Then in your controller:
View:
Option 2: Set in your ViewData dictionary
Controller:
Call Partial View:
View:
您可以只包含要在模型中使用的 id。
You could just include the id you want to use in the model.
就我而言,我想要不同的 ID,因为 jQuery 日历无法将日期写入所需的输入字段。相反,它会将日期写入具有相同 ID 的第一个字段。
所以,我并不真正关心ID。所以用了这样的东西 -
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-