Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net MVC动态Html属性_C#_Html_Asp.net Mvc - Fatal编程技术网

C# Asp.net MVC动态Html属性

C# Asp.net MVC动态Html属性,c#,html,asp.net-mvc,C#,Html,Asp.net Mvc,我在我的视图文本框、下拉列表等上有一些元素。它们都有一些独特的属性,就像这样创建的 <%: Html.DropDownListFor(model => model.MyModel.MyType, EnumHelper.GetSelectList< MyType >(),new { @class = "someclass", @someattrt = "someattrt"})%> model.MyModel.MyType,EnumHelper.GetSelectL

我在我的视图文本框、下拉列表等上有一些元素。它们都有一些独特的属性,就像这样创建的

<%: Html.DropDownListFor(model => model.MyModel.MyType, EnumHelper.GetSelectList< MyType >(),new { @class = "someclass", @someattrt = "someattrt"})%>
model.MyModel.MyType,EnumHelper.GetSelectList(),新的{@class=“someclass”,@someattrt=“someattrt”})%>
我想通过将另一个属性设置为禁用来创建页面的只读版本

有人知道我如何使用可以全局设置的变量吗

比如:

If(pageReadOnly){
isReadOnlyAttr  = @disabled = "disabled";
}else 
{
isReadOnlyAttr   =”” 
} 

<%: Html.DropDownListFor(model => model.MyModel.MyType, EnumHelper.GetSelectList< MyType >(),new { @class = "someclass", @someattrt = "someattrt",isReadOnlyAttr})%>
If(页面只读){
isReadOnlyAtr=@disabled=“disabled”;
}否则
{
isReadOnlyAtr=“”
} 
model.MyModel.MyType,EnumHelper.GetSelectList(),新的{@class=“someclass”,@someattrt=“someattrt”,isReadOnlyAttr})%>

我不想使用JavaScript来实现这一点,而不是禁用下拉列表,为什么不将其替换为所选选项。。。如果你正在做很多事情,你应该考虑拥有一个只读视图和一个可编辑视图

<% if (Model.IsReadOnly) { %>
    <%= Model.MyModel.MyType %>
<% } else { %>
    <%= Html.DropDownListFor(model => model.MyModel.MyType, EnumHelper.GetSelectList< MyType >(),new { @class = "someclass", someattrt = "someattrt"})%>
<% } %>

model.MyModel.MyType,EnumHelper.GetSelectList(),新的{@class=“someclass”,someattrt=“someattrt”})%>
顺便说一句,如果属性名是保留字(如“class”),则只需用“@”转义它

更新

好的。我确实有一个答案给你——但前提是你在实施之前阅读这些内容

MVC是关于分离关注点的。将逻辑放在视图特别关注的控制器中是对MVC的滥用。请不要这样做。任何特定于视图的内容,比如HTML、属性、布局,这些都不应该出现在“controllerville”中。控制器不必更改,因为您希望更改视图中的某些内容

理解MVC试图实现的目标非常重要,下面的示例打破了整个模式,将视图内容完全放在应用程序中错误的位置

正确的解决方案是有一个“读取”视图和一个“编辑”视图,或者在视图中放入任何条件逻辑。但这里有一种方法可以做你想做的事(

将此属性添加到模型中

public IDictionary<string, object> Attributes { get; set; }
公共IDictionary属性{get;set;}
在控制器中,可以有条件地设置属性:

model.Attributes = new Dictionary<string, object>();
model.Attributes.Add(@"class", "test");
if (isDisabled) {
    model.Attributes.Add("disabled", "true");
}
model.Attributes=newdictionary();
添加(@“类”,“测试”);
如果(已禁用){
model.Attributes.Add(“disabled”、“true”);
}
使用视图中的属性:

<%= Html.TextBoxFor(model => model.SomeValue, Model.Attributes)%>
model.SomeValue,model.Attributes)%%>

我做了一些类似于您在我思考之后所做的事情-基本上我有几个不同的系统用户,其中一组在网站上拥有只读权限。为此,我在每个视图模型上都有一个变量:

public bool Readonly { get; set; }
根据其角色权限在我的模型/业务逻辑层中设置

然后,我为DropDownListFor Html帮助程序创建了一个扩展,它接受一个布尔值,指示下拉列表是否应为只读:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

public static class DropDownListForHelper
{
    public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> dropdownItems, bool disabled)
    {
        object htmlAttributes = null;

        if(disabled)
        {
            htmlAttributes = new {@disabled = "true"};
        }

        return htmlHelper.DropDownListFor<TModel, TProperty>(expression, dropdownItems, htmlAttributes);
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq.Expressions;
使用System.Web.Mvc;
使用System.Web.Mvc.Html;
公共静态类DropDownListForHelper
{
public static MvcHtmlString DropDownListFor(此HtmlHelper HtmlHelper、表达式、IEnumerable dropdownItems、bool已禁用)
{
对象htmlAttributes=null;
如果(禁用)
{
HtmlatAttributes=new{@disabled=“true”};
}
返回htmlHelper.DropDownListFor(表达式、dropdownItems、htmlAttributes);
}
}
请注意,您也可以创建具有更多参数的其他实例

而在我的视图中,我只是导入了html helper扩展的名称空间,然后将视图模型变量readonly传递给DropDownList for html helper:

<%@ Import Namespace="MvcApplication1.Helpers.HtmlHelpers" %>

<%= Html.DropDownListFor(model => model.MyDropDown, Model.MyDropDownSelectList, Model.Readonly)%>

model.MyDropDown,model.MyDropDownSelectList,model.Readonly)%%>

我对TextBoxFor、TextAreaFor和CheckBoxFor也做了同样的操作,它们似乎都工作得很好。希望这能有所帮助。

此视图中有很多内容,我们需要它只是为了“演示”您可以做什么。我试图避免创建一个单独的视图或在每个元素上放置“if”语句。我宁愿从表单中删除submit按钮并添加disabled属性。这可能吗?我添加了一个这样做的示例。仅仅因为这是可能的并不意味着你必须这么做。。。请阅读上面的例子。谢谢你的更新,这是我最初的方法,但是不同的元素有不同的属性,所以我不能传递相同的模型。属性给所有的元素。你可以有不同的属性集合,或者您可以将视图上的字典与模型上禁用的属性字典合并。您好,谢谢您的重播,是的,这就是我想要的