asp.net-mvc asp.net MVC:禁用文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1426221/
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-09-07 23:50:40  来源:igfitidea点击:

asp.net MVC: disable a TextBox

asp.net-mvc

提问by Palantir

I have a Html.TextBox() and I need to make it disabled in certain conditions. With the TextArea it goes like this:

我有一个 Html.TextBox(),我需要在某些条件下禁用它。对于 TextArea,它是这样的:

<%=primaryLang ? Html.TextArea("e.location", new { rows = 2, cols = 60 }) : Html.TextArea("e.location", new { rows = 2, cols = 60, disabled = "true" })%>

But with TextBox it is not possible:

但是使用 TextBox 是不可能的:

    <%=primaryLang ? 
      Html.TextBox("e.startDate") : 
        Html.TextBox("e.startDate", new { disabled = "true"})%>

It will issue {disabled=true} in the value. This is because the only function which will allow you to pass the HtmlAttributes will require also the model to be passed. This view is strongly typed, and the model is automatically filled in.

它将在值中发出 {disabled=true}。这是因为唯一允许您传递 HtmlAttributes 的函数也需要传递模型。这个视图是强类型的,模型是自动填充的。

If I pass it like this:

如果我像这样通过它:

Html.TextBox("e.startDate", Model.e.startDate, new { disabled = "true"})

or like this:

或者像这样:

Html.TextBox("e.startDate", null, new { disabled = "true"})

the GET version will work, but the POST version will issue a NullReferenceException. Both the above seem to have the exact same effect. Both will present the correct data from the model on GET.

GET 版本将起作用,但 POST 版本将发出 NullReferenceException。以上两者似乎具有完全相同的效果。两者都将在 GET 上显示来自模型的正确数据。

If I leave it lust like this:

如果我像这样离开它的欲望:

Html.TextBox("e.startDate") 

it will work correctly, for both POST and GET...

它可以正常工作,对于 POST 和 GET ...

Why? Any ways to accomplish?

为什么?有什么方法可以实现吗?

Thanks! :)

谢谢!:)



Thanks to the answers below, I solved it like this:

感谢下面的答案,我是这样解决的:

<%=primaryLang ? 
        Html.TextBox("e.startDate") : 
          Html.Hidden("e.startDate") + Html.TextBox("e.startDate", null, new { disabled = "true"})%>

采纳答案by David Glenn

Disabled HTML elements do not post back to the server. This is why you get a NullReferenceException when you manage to disable your element.

禁用的 HTML 元素不会回发到服务器。这就是为什么当您设法禁用元素时会收到 NullReferenceException 的原因。

I'm not sure what you're trying to achieve but if you are not allowing e.startDate to be editable then you shouldn't need it to be posted back as you should already know the value. So you have two options.

我不确定您要实现的目标,但是如果您不允许 e.startDate 可编辑,那么您不需要将其发回,因为您应该已经知道该值。所以你有两个选择。

  1. Display e.startDate as you were, but just set the value e.startDate in your post method to the default or ignore it completely.
  2. If you need the value posted back then display e.startDate as a label, then add a hidden field containing e.startDate for your posted back value.
  1. 按原样显示 e.startDate,但只需将 post 方法中的值 e.startDate 设置为默认值或完全忽略它。
  2. 如果您需要回传值,则将 e.startDate 显示为标签,然后为回传值添加一个包含 e.startDate 的隐藏字段。

Warning: Just because the element is disabled it doesn't mean that someone can't edit the value and post it back. It's just a recommendation. It's up to the browser how to display the field. If your POST code does accept the e.startDate value then anyone with access can edit that field using development tools.

警告:仅仅因为该元素被禁用并不意味着有人无法编辑该值并将其发回。这只是一个建议。如何显示该字段取决于浏览器。如果您的 POST 代码接受 e.startDate 值,那么任何具有访问权限的人都可以使用开发工具编辑该字段。

回答by Jan Willem B

On the POST, the "Model" or "e" property is probably null.

在 POST 中,“Model”或“e”属性可能为空。

You could try this:

你可以试试这个:

<%if (Model != null && Model.e != null) { %>
<%=Html.TextBox("e.StartDate", Model.e.StartDate, primaryLang ? null :  new { disabled = "disabled" })%>
<%}%>