我有一个 View 模板以及大量 JQuery,它们从 EditorFor
中提取输入。 。因此,我宁愿继续使用我的 EditorFor
而不是使用类似 <input type="text">
的东西
我现在已经实现了一个日期选择器,我所需要的只是一个 ID
属性以使其发挥其魔力( $('#datepicker').datepicker();
),所以我想知道是否有任何方法可以强制 id
在 EditorFor 上?
这是我迄今为止的尝试。
@Html.EditorFor(modelItem => item.ReleaseDate, new {@id ="datepicker" })
如果“应该”有效,是否还有其他原因可能导致该问题?
我使用<input>
在同一 View 中使用此 datepicker 的 modal 标记很好,所以我知道它可以工作,但我根本无法获取 EditorFor
来使用它。
谢谢!
编辑:@ForEach
将每个项目的 datepicker 添加到每个项目的 releaseDate 中。 (第二个td
下)
@foreach (var item in Model)
{
<tr>
<td class="col-lg-2">
<span class="item-display">
<span style="font-size: 17px;">
@Html.DisplayFor(modelItem => item.Name)
</span>
</span>
<span class="item-field">
@Html.EditorFor(modelItem => item.Name)
</span>
</td>
<td class="col-lg-3">
<span class="item-display">
<span style="font-size: 17px;">
@Html.DisplayFor(modelItem => item.ReleaseDate)
</span>
</span>
<span class="item-field">
@Html.TextBoxFor(modelItem => item.ReleaseDate, new {@id="datepicker"})
@*@Html.EditorFor(modelItem => item.ReleaseDate, new {@id ="datepicker" })*@
</span>
</td>
<td class="col-lg-3">
<span class="item-display">
<span style="font-size: 17px; font-style:italic">
@Html.DisplayFor(modelItem => item.Description)
</span>
</span>
<span class="item-field">
@Html.EditorFor(modelItem => item.Description)
</span>
</td>
<td class="col-lg-3 col-lg-offset-1">
<span style="visibility:hidden" class="ID col-lg-1">@Html.DisplayFor(modelItem => item.ID)</span>
<span class="item-edit-button">
<button type="button" onclick="editFunction(this)" class=" btn btn-warning col-lg-3 col-lg-offset-0"><span style="margin-right: 5px" class="glyphicon glyphicon-pencil"></span>Edit</button>
</span>
<span class="item-save-button">
<button type="button" onclick="saveFunction(this)" class="btn btn-success col-lg-3 col-lg-offset-4"><span style="margin-right: 5px" class="glyphicon glyphicon-trash"></span>Save</button>
</span>
<!-- Modal Button-->
<span class="item-delete-button">
<button class="btn btn-danger col-lg-3 col-lg-offset-3" data-toggle="modal" data-target="#@item.ID" onclick="deleteStart(this)">
<span style="margin-right: 5px" class="glyphicon glyphicon-trash"></span>Delete
</button>
</span>
<!-- Modal -->
<div class="modal fade" id="@item.ID" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style="background-color: #d9534f">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel" style="font-size: 30px; font-style:oblique; color:white">Delete</h4>
</div>
<div class="modal-body">
<span style="font-size: 20px">Are you sure you want to delete movie: @Html.DisplayFor(modelItem => item.Name)?</span>
</div>
<div class="modal-footer">
<button type="button" onclick="deleteStopped(this)" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" style="margin-right:10px" onclick="deleteFunction(this)" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td colspan="12">
<p style="font-size: 17px; font-style: italic; font-family: 'Roboto', sans-serif">
Movie ID: @Html.DisplayFor(modelItem => item.ID)
<br />
Placeholder
</p>
</td>
</tr>
}
最佳答案
Html.EditorFor()
帮助程序没有采用 html 属性的重载,请使用 Html.TextBoxFor()
帮助程序:
@Html.TextBoxFor(modelItem => item.ReleaseDate, new {@id ="datepicker" })
这里是MSDN Html.EditorFor()
和 here 的文档用于Html.TextBoxFor()
注意:
当你写下:
@Html.EditorFor(modelItem => item.ReleaseDate)
它的渲染如下:
<input type="text" name="ReleaseDate" id="ReleaseDate"/>
因此它将 id 和 name 属性设置为 Model 属性,因此如果您想使用 Html.EditorFor()
帮助程序,您只需使用该 id 即可。
关于javascript - 为 EditorFor 提供 ID 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24066038/