修改 ASP.NET MVC 2 中的 HTML 帮助程序
我想修改像这样的帮助器:
<%= Html.CheckBoxFor(m => m.Current, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%>
同时将另一个表示应用程序中的权限的字符串作为参数,然后在方法内部我将确定是否返回实际的 HTML 或不返回任何内容,具体取决于他们的权限。
我该怎么做?
更新 2:复选框未呈现为只读
当我调试并检查 htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes)._value 的值时,我得到了这一点,
<input checked="checked" class="economicTextBox" id="Current" name="Current" onchange="UseCurrent();UpdateField(this);" propertyName="Current" readonly="true" type="checkbox" value="true" /><input name="Current" type="hidden" value="false" />
但复选框仍在呈现,允许我更改它并实现完整功能。为什么?
I want to modify helpers such as this one:
<%= Html.CheckBoxFor(m => m.Current, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%>
to also take as a parameter another string that signifies a permission in the app, and then INSIDE the method I would determine whether or not to return the actual HTML or nothing, depending on their permission.
How would I do this?
UPDATE 2: Checkbox not rendering as readonly
When I debug and check the value of htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes)._value, I get this
<input checked="checked" class="economicTextBox" id="Current" name="Current" onchange="UseCurrent();UpdateField(this);" propertyName="Current" readonly="true" type="checkbox" value="true" /><input name="Current" type="hidden" value="false" />
but the checkbox is still rendering allowing me to change it and achieve full functionality. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写一个自定义帮助程序:
然后:
更新:
以下是您可以修改 HTML 帮助程序的方法,以便在用户没有权限时呈现只读复选框而不是空字符串:
You could write a custom helper:
and then:
UPDATE:
Here's how you could modify the HTML helper so that it renders a readonly checkbox instead of an empty string if the user has no permissions:
为了执行您想要的操作,您需要创建自己的 HTML Helper。 HTML Helper 方法只是扩展方法。因此,您可以轻松创建自己的执行适当的权限检查,然后如果通过,则使用其余参数调用默认的 Html.CheckBoxFor。
前面的问题有一个创建自定义帮助器的不错的示例。
In order to do what you want, you need to create you own HTML Helper. The HTML Helper methods are just extension methods. As such you can easily create your own that does the proper permission checking and then if it passes, call the default Html.CheckBoxFor with the rest of the parameters.
This previous question has a decent example of creating custom helpers.