Asp.net 设置属性";只读“;for@Html.TextBoxFor()根据从db获取的信息动态更新

Asp.net 设置属性";只读“;for@Html.TextBoxFor()根据从db获取的信息动态更新,asp.net,asp.net-mvc,entity-framework,razor,html.textboxfor,Asp.net,Asp.net Mvc,Entity Framework,Razor,Html.textboxfor,我使用@Html.TextBoxFor()定义了多个文本框。现在,根据用户访问页面的角色,我希望其中一些是只读的,而另一些是可编辑的 我试过使用以下方法 @Html.TextBoxFor(f => f.VSSLabel, new { style = "height:19px", @Value = @ViewBag.fetchf.VSSLabel, @readonly="readonly" }) 是否有任何方法可以设置@readonly=“false”并使其变为可编辑,或者有任何其他方法使

我使用@Html.TextBoxFor()定义了多个文本框。现在,根据用户访问页面的角色,我希望其中一些是只读的,而另一些是可编辑的

我试过使用以下方法

@Html.TextBoxFor(f => f.VSSLabel, new { style = "height:19px", @Value = @ViewBag.fetchf.VSSLabel, @readonly="readonly" })

是否有任何方法可以设置@readonly=“false”并使其变为可编辑,或者有任何其他方法使我将其切换为“readonly”并基于存储在来自控制器的ViewBag变量中的值进行编辑

不幸的是,下面的所有标记都将呈现只读文本框输入

<input type="text" name="s1" readonly="readonly"/>
<input type="text" name="s2" readonly="no" />
<input type="text" name="s2" readonly="reallyDoNotWant" />
<input type="text" name="s3" readonly="false" />
<input type="text" name="s4" readonly />
如果要将其与viewbag字典项进行检查

if (ViewBag.IsAdmin !=null && ViewBag.IsAdmin)
{
    @Html.TextBoxFor(a => a.VSSLabel)
}
else
{
    @Html.TextBoxFor(a => a.VSSLabel, new { @readonly = "readonly" })
}

假设您在操作方法中将
ViewBag.IsAdmin
设置为布尔值。

不幸的是,下面的所有标记将呈现只读文本框输入

<input type="text" name="s1" readonly="readonly"/>
<input type="text" name="s2" readonly="no" />
<input type="text" name="s2" readonly="reallyDoNotWant" />
<input type="text" name="s3" readonly="false" />
<input type="text" name="s4" readonly />
如果要将其与viewbag字典项进行检查

if (ViewBag.IsAdmin !=null && ViewBag.IsAdmin)
{
    @Html.TextBoxFor(a => a.VSSLabel)
}
else
{
    @Html.TextBoxFor(a => a.VSSLabel, new { @readonly = "readonly" })
}

假设您在操作方法中将
ViewBag.IsAdmin
设置为布尔值。

为了使代码更易于阅读,您可以使用可以声明的函数:

@functions
{
    private object GetAttributes()
    {
        if (ViewBag.IsAdmin !=null && ViewBag.IsAdmin)
        {
            return null;
        }

        return new { @readonly = "readonly" };
    }
}
然后你可以这样使用它:

@Html.TextBoxFor(a => a.VSSLabel, GetAttributes())
/* for .NET Core       */ using Microsoft.AspNetCore.Mvc.ViewFeatures;
/* for .NET Framework: */ using System.Web.WebPages.Html;

public static class HtmlHelpers
{
    public static object MakeReadonly(this object htmlAttributes, bool isReadonly)
    {
        if (isReadonly)
        {
            var dynamicHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            dynamicHtmlAttributes["readonly"] = "readonly";
            return dynamicHtmlAttributes;
        }

        return htmlAttributes;
    }
}
在函数中,您可以添加需要添加到元素的任何属性:

return new { @class = "form-control", @readonly = "readonly", @required = "required" }

而且它可以完美地工作

为了使您的代码更易于阅读,您可以使用您可以声明的函数:

@functions
{
    private object GetAttributes()
    {
        if (ViewBag.IsAdmin !=null && ViewBag.IsAdmin)
        {
            return null;
        }

        return new { @readonly = "readonly" };
    }
}
然后你可以这样使用它:

@Html.TextBoxFor(a => a.VSSLabel, GetAttributes())
/* for .NET Core       */ using Microsoft.AspNetCore.Mvc.ViewFeatures;
/* for .NET Framework: */ using System.Web.WebPages.Html;

public static class HtmlHelpers
{
    public static object MakeReadonly(this object htmlAttributes, bool isReadonly)
    {
        if (isReadonly)
        {
            var dynamicHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            dynamicHtmlAttributes["readonly"] = "readonly";
            return dynamicHtmlAttributes;
        }

        return htmlAttributes;
    }
}
在函数中,您可以添加需要添加到元素的任何属性:

return new { @class = "form-control", @readonly = "readonly", @required = "required" }

它工作得非常完美

您可以编写如下扩展方法:

@Html.TextBoxFor(a => a.VSSLabel, GetAttributes())
/* for .NET Core       */ using Microsoft.AspNetCore.Mvc.ViewFeatures;
/* for .NET Framework: */ using System.Web.WebPages.Html;

public static class HtmlHelpers
{
    public static object MakeReadonly(this object htmlAttributes, bool isReadonly)
    {
        if (isReadonly)
        {
            var dynamicHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            dynamicHtmlAttributes["readonly"] = "readonly";
            return dynamicHtmlAttributes;
        }

        return htmlAttributes;
    }
}
用法:

@Html.TextBoxFor(..., new { @class = "form-control" }.MakeReadonly(true))
这种方法的一个缺点是,
对象
上的扩展方法有点可疑,因为它们在IntelliSense中到处都会弹出


如果您不喜欢这样,我建议将
htmlAttributes
从匿名对象更改为
ViewDataDictionary
,并使扩展方法与之配合使用。

您可以编写如下扩展方法:

@Html.TextBoxFor(a => a.VSSLabel, GetAttributes())
/* for .NET Core       */ using Microsoft.AspNetCore.Mvc.ViewFeatures;
/* for .NET Framework: */ using System.Web.WebPages.Html;

public static class HtmlHelpers
{
    public static object MakeReadonly(this object htmlAttributes, bool isReadonly)
    {
        if (isReadonly)
        {
            var dynamicHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            dynamicHtmlAttributes["readonly"] = "readonly";
            return dynamicHtmlAttributes;
        }

        return htmlAttributes;
    }
}
用法:

@Html.TextBoxFor(..., new { @class = "form-control" }.MakeReadonly(true))
这种方法的一个缺点是,
对象
上的扩展方法有点可疑,因为它们在IntelliSense中到处都会弹出


如果您不喜欢,我建议将
htmlAttributes
从匿名对象更改为
ViewDataDictionary
,并使扩展方法与之配合使用。

Shyju所说的是正确的,但是Shariq Ali是正确的,如果您有很多字段要执行,Razor代码会变得非常低效

在我的例子中,我有一个完整的表单,我想在某些情况下使其成为只读的。我发现的解决问题的方法之一就是少编一点代码

@{
object htmlAttr = null;

if ( ViewBag.AllowEdit != null && !ViewBag.AllowEdit ){
    htmlAttr = new { @class="CSS", @readonly="readonly"};
}
else {
    htmlAttr = new { @class="CSS" };
}

@Html.TextBoxFor( m => m.Field, htmlAttr)
由于表单中的大多数编辑控件都带有相同的CSS类,因此应该能够满足大多数需要。如果您发现在某些控件上需要更多类,只需添加额外的htmlAttribute对象来承载不同的类配置


通过使用描述性变量名,这集中了只读的逻辑,并使razor页面更加简洁

Shyju的说法是正确的,但是Shariq Ali是正确的,如果你有很多字段要做,Razor代码就会变得非常低效

在我的例子中,我有一个完整的表单,我想在某些情况下使其成为只读的。我发现的解决问题的方法之一就是少编一点代码

@{
object htmlAttr = null;

if ( ViewBag.AllowEdit != null && !ViewBag.AllowEdit ){
    htmlAttr = new { @class="CSS", @readonly="readonly"};
}
else {
    htmlAttr = new { @class="CSS" };
}

@Html.TextBoxFor( m => m.Field, htmlAttr)
由于表单中的大多数编辑控件都带有相同的CSS类,因此应该能够满足大多数需要。如果您发现在某些控件上需要更多类,只需添加额外的htmlAttribute对象来承载不同的类配置


通过使用描述性变量名,这集中了只读的逻辑,并使razor页面更加简洁

我认为这不是一个好办法。我可以转到开发工具,删除readonly属性,更改值并提交表单。我遇到过这种方法,但我发现这种方法太长,代码效率低下。那么有没有其他的办法Shyju?我认为这不是一个好办法。我可以转到开发工具,删除readonly属性,更改值并提交表单。我遇到过这种方法,但我发现这种方法太长,代码效率低下。那么,还有别的办法吗?