Giving EditorFor an ID Attribute

℡╲_俬逩灬. 提交于 2019-12-25 02:46:48

问题


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">

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?

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.

Thank you!

EDIT: @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>

        }

回答1:


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

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

Here is MSDN documentation for Html.EditorFor() and here is for Html.TextBoxFor()

NOTE:

When you write:

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

It is rendered like this:

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

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.



来源:https://stackoverflow.com/questions/24066038/giving-editorfor-an-id-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!