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视图页面中表单中存在的所有控件(文本框,复选框,按钮等)。有没有办法做到这一点?请帮忙。
3 个解决方案
#1
10
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>
#2
1
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.
这实际上取决于控件的呈现方式。我们在实践中做了类似的事情,除了我们将控件设置为只读。这是为了让我们重新使用show(只读)和编辑视图。
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的初期,我不建议这样做,但它是另一种选择。
#3
0
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库来执行某些操作。
#1
10
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>
#2
1
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.
这实际上取决于控件的呈现方式。我们在实践中做了类似的事情,除了我们将控件设置为只读。这是为了让我们重新使用show(只读)和编辑视图。
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的初期,我不建议这样做,但它是另一种选择。
#3
0
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库来执行某些操作。