使用 Html.TextBoxFor 时,我可以将文本框设置为只读吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2539069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:35:42  来源:igfitidea点击:

Can I set text box to readonly when using Html.TextBoxFor?

.nethtmlasp.net-mvc

提问by AwkwardCoder

I have the following tag with a Html.TextBoxFor expression and I want the contents to be read only, is this possible?

我有以下带有 Html.TextBoxFor 表达式的标签,我希望内容是只读的,这可能吗?

<%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action)%>

采纳答案by Jaxidian

Updated for modern versions of .NET per @1c1cle's suggestion in a comment:

根据@1c1cle 在评论中的建议,针对现代版本的 .NET 进行了更新:

<%= Html.TextBoxFor(model => Model.SomeFieldName, new {{"readonly", "true"}}) %>

Do realize that this is not a "secure" way to do this as somebody can inject javascript to change this.

请意识到这不是一种“安全”的方式来做到这一点,因为有人可以注入 javascript 来改变这一点。

Something to be aware of is that if you set that readonlyvalue to false, you actually won't see any change in behavior! So if you need to drive this based on a variable, you cannot simply plug that variable in there. Instead you need to use conditional logic to simply not pass that readonlyattribute in.

需要注意的是,如果您将该readonly值设置为false,您实际上不会看到任何行为变化!因此,如果您需要根据变量来驱动它,则不能简单地将该变量插入其中。相反,您需要使用条件逻辑来简单地不传入该readonly属性。

Here is an untested suggestion for how to do this (if there's a problem with this, you can always do an if/else):

这是关于如何执行此操作的未经测试的建议(如果有问题,您可以随时执行 if/else):

<%= Html.TextBoxFor(model => Model.SomeFieldName, shouldBeReadOnlyBoolean ? new {{"readonly", "true"}} : null) %>

回答by Dan

<%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new { @readonly = true })%>

回答by Ruaan Volschenk

Use the following:

使用以下内容:

 @Html.TextBoxFor(m => m.Whatever, new {@readonly = "readonly"})

If you want to assign a class to it you could do it the same way , by adding the @class = "" property. Hope this helps :)

如果你想给它分配一个类,你可以通过添加@class = "" 属性来做同样的事情。希望这可以帮助 :)

回答by Ali Adravi

To make it read only

使其只读

@Html.TextBoxFor(m=> m.Total, new {@class ="form-control", @readonly="true"})

To diable

禁用

@Html.TextBoxFor(m=> m.Total, new {@class ="form-control", @disabled="true"})

回答by Pàldi Gerg?

The following snippet worked for me.

以下代码段对我有用。

@Html.TextBoxFor(m => m.Crown, new { id = "", @style = "padding-left:5px", @readonly = "true" }) 

回答by Matt Dearing

<%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new {readonly=true})%>

回答by user1021124

This work for me...

这项工作对我来说...

@Html.TextBoxFor(model => model.RIF, new { value = @Model.RIF, @readonly = "readonly" })

回答by Zyon

<%: Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new { @autocomplete = "off", @readonly=true})%>

This is how you set multiple properties

这是您设置多个属性的方式

回答by hbpatrick81

An other possibility :

另一种可能性:

<%= Html.TextBoxFor(model => Model.SomeFieldName, new Dictionary<string, object>(){{"disabled", "true"}}) %>

回答by Sheriff

You can also disabled TextBoxFor

您还可以禁用 TextBoxFor

@Html.TextBoxFor(x=>x.day, null, new { @class = "form-control success" ,@disabled="disabled" })