C# 禁用视图 (ASP.NET MVC) 中的所有控件(文本框、复选框、按钮等)

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

Disable all controls (textbox, checkbox, button etc) in a View (ASP.NET MVC)

c#asp.netasp.net-mvc

提问by Ravi

While rendering the view page, based on some condition in the controller action I want to disable all the controls (textbox, checkbox, button etc) present in the form in a MVC view page. Is there any way to do that? Please help.

在呈现视图页面时,基于控制器操作中的某些条件,我想禁用 MVC 视图页面中表单中存在的所有控件(文本框、复选框、按钮等)。有没有办法做到这一点?请帮忙。

采纳答案by Annath

you can pass a flag to the view to indcate that it must disable all the controls.

您可以将标志传递给视图以指示它必须禁用所有控件。

here is an example:

这是一个例子:

public ActionResult MyAction() {
 ViewData["disablecontrols"] = false;
 if (condition)
 {
    ViewData["disablecontrols"] = true;
 }
 return View();
}

In the view(using jQuery):

在视图中(使用 jQuery):

   <script type="text/javascript">
$(document).ready(function() {
var disabled = <%=ViewData["disablecontrols"].ToString()%>;
  if (disabled) {
    $('input,select').attr('disabled',disabled);
  }
})
    </script>

回答by Annath

I don't think you can do that from the controller, since the view is returned after all the other logic is done. You could probably do something, however, with the AJAX libraries included with ASP.NET MVC.

我认为您不能从控制器执行此操作,因为在完成所有其他逻辑后返回视图。但是,您可能可以使用 ASP.NET MVC 中包含的 AJAX 库做一些事情。

回答by Samantha Branham

That really depends on how your controls are being rendered. We do something similar in practice, except we set controls to read only. This is to allow us to re-use show (read-only) and edit views.

这实际上取决于您的控件是如何呈现的。我们在实践中做了类似的事情,除了我们将控件设置为只读。这是为了允许我们重用显示(只读)和编辑视图。

The way I would personally recommend to do it is to have a read-only flag that is set in the view using a value in ViewData.

我个人建议这样做的方法是使用 ViewData 中的值在视图中设置一个只读标志。

From there, write some helper methods to distinguish between disabled and non-disabled markup. You can build this markup yourself, or wrap the existing HtmlHelper methods ASP.NET MVC provides.

从那里,编写一些辅助方法来区分禁用和非禁用标记。您可以自己构建此标记,或包装 ASP.NET MVC 提供的现有 HtmlHelper 方法。

// In your controller
ViewData["DisableControls"] = true;

<%-- In your view --%>
<% bool disabled = ViewData["DisableControls"] as bool; %>
...
<%= Html.TextBox("fieldname", value, disabled) %>
<%= Html.CheckBox("anotherone", value, disabled) %>

// In a helper class
public static string TextBox(this HtmlHelper Html, string fieldname, object value, bool disabled)
{
    var attributes = new Dictionary<string, string>();
    if (disabled)
        attributes.Add("disabled", "disabled");
    return Html.TextBox(fieldname, value, attributes);
}

The way we do it is to use the Page_Load(), as you would in WebForms, to disable server controls. We built some custom server controls to handle our form fields. This was in ASP.NET MVC's infancy, and I wouldn't recommend doing this, but it's an alternative.

我们这样做的方法是使用 Page_Load() 来禁用服务器控件,就像在 WebForms 中一样。我们构建了一些自定义服务器控件来处理我们的表单字段。这是在 ASP.NET MVC 的初期,我不建议这样做,但它是一种替代方法。