简体   繁体   English

如何使用Html.EditorFor使用Razor最初是只读文本框?

[英]How to use Html.EditorFor to initially be read only text box using Razor?

I want to create an output with several rows of data in populated textboxes that are initialy read only, each with an Edit button at the end. 我想在最初是只读的填充文本框中创建包含几行数据的输出,每行的末尾都有一个“ 编辑”按钮。 This edit would allow that row to become editable while staying on the same page (no new window which is the default with MVC) and then when all edits are complete a submit button at the bottom of the page would save the data to the database. 此编辑将允许该行在保留在同一页面上时变为可编辑的(没有新窗口,这是MVC的默认窗口),然后在完成所有编辑后,页面底部的提交按钮会将数据保存到数据库中。 @Html.DisplayFor() will display as read only but to edit this, a new page is created. @Html.DisplayFor()将显示为只读,但要对其进行编辑,将创建一个新页面。

If somehow clicking edit could transform that row from @Html.DisplayFor() to @Html.EditorFor() that would be great. 如果以某种方式单击“编辑”可以将该行从@Html.DisplayFor()@Html.EditorFor() ,那就太好了。

Here's some code snippets: 以下是一些代码片段:

@model IEnumerable<EnterAdv.Models.Person>
@using (Html.BeginForm()){
<fieldset>
    <table>
        @foreach (var item in Model)
        {
            <tr>
                <td 
                    class="styleDeptNames">@Html.EditorFor(modelItem => item.FirstName) 
                                            @Html.ValidationMessageFor(model => item.FirstName)
                </td>
                <td 
                    class="styleFMLNames">@Html.EditorFor(modelItem => item.LastName)
                                            @Html.ValidationMessageFor(model => item.LastName)
                </td>
                <td class="styleEdit"><input type="button" value="Edit"/></td>     
            </tr>
        }
    </table>
</fieldset>

As you can see right now it's EditorFor() . 如您现在所见,它是EditorFor() If this method had an overload that allowed to switch back and forth between read only and edit that would be great but I don't see it. 如果此方法有重载,并且允许在只读和编辑之间来回切换,那将是很好的选择,但我看不到。 But the bottom line is I have to stay on the same page to edit. 但最重要的是我必须留在同一页面上进行编辑。

I've been using ActionResult Index() View since it lists the table data. 我一直在使用ActionResult Index()视图,因为它列出了表数据。

you can toggle the read only using jquery 您可以使用jQuery切换只读

$('.clsInput').attr('readonly', 'readonly');

and to remove it 并删除它

$('.clsInput').removeAttr('readonly');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM