给编辑者一个ID属性 [英] Giving EditorFor an ID Attribute

查看:236
本文介绍了给编辑者一个ID属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图的模板,还有很多JQuery从 EditorFor 中提取输入。因此,我宁愿继续使用我的 EditorFor ,而不是使用类似< input type =text>

I have a template for Views along with lots of JQuery that pulls input from a EditorFor. For this reason I'd prefer to keep using my EditorFor rather than use something like <input type="text">

我现在已经实现了一个 datepicker ,只需要一个 ID 属性为了它的魔法( $('#datepicker')。datepicker(); ),所以我想知道有没有办法强制一个 id 在编辑器上?

I now have implemented a datepicker and all I need is an ID attribute in order for it to do its magic ( $('#datepicker').datepicker(); ), so I am wondering is there any way to force an id on an EditorFor?

这是我到目前为止的尝试。

Here is my attempt thus far.

@Html.EditorFor(modelItem => item.ReleaseDate, new {@id ="datepicker" })

如果应该运行,还有其他可能导致问题的问题吗?

And if that "should" be working is there something else that could be causing the issue?

我在同一个视图中使用模式< input> 标签它使用这个 datepicker 很好,所以我知道它的工作,但我根本无法获得 EditorFor 来处理它。

I use an <input> tag for a modal in this same View which uses this datepicker just fine so I know it works, but I simply cannot get the EditorFor to work with it.

谢谢!

编辑: @ForEach datepicker到每个项目的releaseDate。 (第二个 td down)

@ForEach that takes each item that add's datepicker to the releaseDate of each item. (2nd td down)

 @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">&times;</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() helper不具有html属性的重载,使用 Html.TextBoxFor() helper:

Html.EditorFor() helper don't have overload that takes html attributes, use Html.TextBoxFor() helper:

@Html.TextBoxFor(modelItem => item.ReleaseDate, new {@id ="datepicker" })

这里是 Html.EditorFor()的%28v = vs.118%29.aspxrel =nofollow> MSDN 文档和 这里 用于 Html.TextBoxFor()

当你写:

@Html.EditorFor(modelItem => item.ReleaseDate)

它呈现如下:

<input type="text" name="ReleaseDate" id="ReleaseDate"/>

所以它将id和name属性设置为Model属性,所以你可以使用该id想使用 Html.EditorFor() helper。

so it sets the id and name attribute to Model property,so you just can use that id if you want to use Html.EditorFor() helper.

这篇关于给编辑者一个ID属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆